Skip to content
Email jcinfo@juniorcoderz.com

Python

Python Functions Explained for Beginners (With Examples)

  • Junior Coderz Team
  • 6 min read
Python Functions Explained for Beginners (With Examples): Junior Coderz blog cover

Python functions are one of the first big ideas every new coder meets, and one of the most useful. A function lets you write a piece of code once, give it a name, and use it again whenever you need it.

This beginner-friendly guide explains what functions in Python are, how to write your own, how parameters and return values work, and the common mistakes to avoid. Every example is short, so you can type it in and try it yourself.

What Is a Function in Python?

A function is a named block of code that does one job. Think of it like a recipe card: once the recipe for "make a sandwich" is written down, you don't need to explain every step again; you just say "make a sandwich". In Python, you write the steps once inside a function, then "call" the function by its name to run those steps. Python already comes with many built-in functions, and you have probably used some of them already.

Built-in functionWhat it doesExample
print()Shows text on the screenprint("Hi!")
input()Asks the user to type somethinginput("Your name? ")
len()Counts the items or letterslen("Python") gives 6
round()Rounds a numberround(3.7) gives 4

How to Write Your Own Python Functions

The real power comes when you create your own functions. Here is how, step by step.

1. Define a Function With def

You create a function with the word def (short for "define"), then the function's name, brackets and a colon. The code inside the function is indented by four spaces.

def say_hello():
    print("Hello, coder!")
    print("Let's build something today.")

Nothing happens yet. You have only written the recipe; you haven't cooked it.

2. Call the Function

To run the function, write its name followed by brackets. You can call it as many times as you like.

say_hello()
say_hello()

This prints the two lines twice, without writing them again.

Book a Free Trial Class

3. Give It Information With Parameters

Parameters let you send information into a function, so it can do slightly different things each time. The parameter goes inside the brackets.

def greet(name):
    print("Hello, " + name + "!")

greet("Ali")
greet("Sara")

The output is "Hello, Ali!" and then "Hello, Sara!". The same function, two different results. The value you send in, such as "Ali", is called an argument.

4. Use More Than One Parameter

A function can take several parameters, separated by commas.

def area_of_rectangle(width, height):
    print("The area is", width * height)

area_of_rectangle(5, 3)

This prints "The area is 15".

5. Send an Answer Back With return

Sometimes you want a function to give you an answer you can use later, instead of just printing it. That is what return does.

def add(a, b):
    return a + b

total = add(4, 6)
print(total * 2)

The function returns 10, which is stored in total, and the program prints 20. Returning values is what lets functions work together like building blocks.

6. Set Default Values

You can give a parameter a default value, which is used if no argument is sent.

def power_up(player, points=10):
    print(player, "gets", points, "points!")

power_up("Maya")
power_up("Omar", 50)

Maya gets the default 10 points, while Omar gets 50.

Why Are Functions So Useful?

  • No repeating yourself. Write the code once and use it many times.
  • Easier to read. A name like check_answer() tells you what the code does at a glance.
  • Easier to fix. If there is a bug, you fix it in one place, and every call is fixed too.
  • Bigger projects become possible. Games, apps and AI programs are built from many small functions working together.

Mini Project: A Quiz Using Functions

Here is a small quiz that uses a function with parameters and a return value. Try adding your own questions.

def ask(question, answer):
    reply = input(question + " ")
    if reply.lower() == answer:
        print("Correct!")
        return 1
    print("Not quite. The answer is", answer)
    return 0

score = 0
score += ask("What is 7 x 8?", "56")
score += ask("Which planet is known as the Red Planet?", "mars")
score += ask("How many sides does a hexagon have?", "6")
print("You scored", score, "out of 3")

The ask() function does the same job three times: it asks, checks and returns a point. Without a function, you would need to write those lines three times. Want more ideas? See our list of Python projects for beginners or build a simple Python calculator, which also uses functions.

Common Mistakes Beginners Make With Functions

MistakeWhat happensHow to fix it
Forgetting the colon after def name()Python shows a SyntaxErrorAlways end the def line with :
Not indenting the code insidePython shows an IndentationErrorIndent the function's code by four spaces
Defining but never callingNothing happens when the program runsCall the function by its name with brackets
Calling before definingPython says the name is not definedWrite the def above the line that calls it
Printing instead of returningYou can't use the answer laterUse return when you need the result

Tips for Writing Good Functions

Give each function one clear job, and name it with a verb, such as draw_star() or calculate_total(). Keep functions short; if one grows very long, split it into two. Use lowercase names with underscores between words, which is the normal Python style. And test each function on its own before using it in a bigger program. If you are completely new to Python, start with our guide on how to start with Python.

Build Real Python Projects at Junior Coderz

At Junior Coderz, students learn Python step by step in live online classes with engineer instructors. They start with the basics, such as variables, loops and functions, then use them to build games, quizzes, chatbots and simple AI projects. Our Python classes for kids suit beginners from around age 10, and younger children can begin with Scratch. Read more in our guide to Python for kids, or see every option on our courses page.

Write Your First Python Function With an Engineer

Functions click much faster when someone shows you live. In a free trial class, your child writes real Python code with one of our engineer instructors and builds a small project they can keep improving at home. You also get honest advice on the right level and next steps.

  • Real Python Projects
  • Engineer Instructors
  • Live Online Classes

Book a Free Trial Class

Small Blocks, Big Programs

Once you understand Python functions, coding starts to feel like building with blocks. You make small pieces that each do one job, then snap them together into games, apps and tools. Start with a simple greeting function, add parameters, then return values, and soon you will be organising whole projects. To see what young Python coders are building, visit Junior Coderz or follow us on Instagram.

FAQs

What is a function in Python in simple words?

A function is a named block of code that does one job. You write it once and run it whenever you need it by calling its name.

What does def mean in Python?

def is short for "define". It is the word Python uses to create a new function.

What is the difference between a parameter and an argument?

A parameter is the name inside the function's brackets, such as name. An argument is the actual value you send in when you call it, such as "Ali".

What is the difference between print and return?

print() only shows a value on the screen. return sends the value back to the program, so you can store it and use it later.

Can a function call another function?

Yes. Functions often use other functions, which is how bigger programs are built from small, simple parts.

At what age can kids learn Python functions?

Most children can understand functions from around age 10 or 11, once they are comfortable with variables and loops.

Turn Screen Time Into Creation Time

Book a free trial class and see how your child learns to code with Junior Coderz.

Book a Free Trial Class
All articles