Python Functions Explained
Functions are one of the most useful parts of Python.
A function is a named block of code that does a specific job. You define it once, then call it whenever you need it. This helps you avoid repeating the same code and makes programs easier to read.
In this guide, you will learn:
- what a function is
- why functions are useful
- how to define and call a function
- how parameters work at a basic level
- how return values work
If you want a shorter definition first, see what is a function in Python.
What a function is
A function is a reusable block of code.
That means:
- you write the code once
- you give it a name
- you run it when needed
Functions are useful because they let you group related code into one place.
A function can:
- take input values
- do some work
- produce an output value
Here is a very simple function:
def say_hello():
print("Hello")
This creates the function, but it does not run yet.
Why functions are useful
Functions help you write better code.
They are useful because they:
- make code easier to read
- reduce repetition
- break large programs into smaller parts
- make testing and debugging easier
For example, without a function you might repeat the same lines many times:
print("Welcome!")
print("Welcome!")
print("Welcome!")
With a function, you can reuse the same code:
def show_welcome():
print("Welcome!")
show_welcome()
show_welcome()
show_welcome()
This is easier to change later. If you want to update the message, you only change it in one place.
How to define a function
To define a function in Python, use the def keyword.
A basic function definition has these parts:
def- the function name
- parentheses
() - a colon
: - an indented block of code
Example:
def greet():
print("Hello")
What each part means
deftells Python you are creating a functiongreetis the function name()means this function takes no input values:starts the function body- the indented line is the code that belongs to the function
Indentation matters in Python. The code inside the function must be indented.
This will cause an error:
def greet():
print("Hello")
This works correctly:
def greet():
print("Hello")
How to call a function
Calling a function means running it.
To call a function, write its name followed by parentheses:
def greet():
print("Hello")
greet()
Output:
Hello
A function does nothing until it is called.
In this example, the function is defined but never used:
def greet():
print("Hello")
Since there is no greet() call, nothing is printed.
You can call the same function many times:
def greet():
print("Hello")
greet()
greet()
greet()
Output:
Hello
Hello
Hello
Functions with parameters
Parameters are names for input values inside a function.
They let one function work with different data.
Example:
def greet(name):
print("Hello,", name)
greet("Ava")
greet("Noah")
Output:
Hello, Ava
Hello, Noah
In this example:
nameis the parameter"Ava"and"Noah"are arguments
A parameter is the variable name in the function definition.
An argument is the value you pass when calling the function.
This is just the basic idea. For a full beginner guide, see function parameters and arguments in Python.
Return values
Use return when you want a function to send a value back.
That returned value can then be:
- stored in a variable
- used in another expression
- printed later
Example:
def add_numbers(a, b):
return a + b
result = add_numbers(3, 4)
print(result)
Output:
7
print() and return are not the same
This is a very common beginner mistake.
print() shows something on the screen.return sends a value back from the function.
Compare these two functions:
def show_sum(a, b):
print(a + b)
def get_sum(a, b):
return a + b
show_sum(3, 4)
result = get_sum(3, 4)
print(result)
Output:
7
7
The output looks similar, but the functions behave differently.
show_sum()prints the result, but does not return itget_sum()returns the result, so you can store and reuse it
What happens if there is no return
If a function does not use return, Python returns None by default.
def greet():
print("Hello")
result = greet()
print(result)
Output:
Hello
None
If you want to learn this in more detail, see return values in Python functions.
Simple example flow
A good way to understand functions is to follow the usual order:
- define the function
- call the function
- see the result
Here is a simple example:
def multiply_by_two(number):
return number * 2
answer = multiply_by_two(5)
print(answer)
Output:
10
Step by step
def multiply_by_two(number):defines the functionnumberis the inputreturn number * 2sends back the resultmultiply_by_two(5)calls the function with5- the returned value is stored in
answer
Now compare a print-based function and a return-based function:
def print_double(number):
print(number * 2)
def return_double(number):
return number * 2
print_double(5)
value = return_double(5)
print(value)
Output:
10
10
Again, the screen output is similar, but only return_double() gives you a value you can save and use later.
When to use a function
Use a function when:
- the same task appears more than once
- one part of the program should do one clear job
- you want code that is easier to reuse
- a long script needs to be split into smaller pieces
For example, if your program needs to calculate tax in several places, a function is better than repeating the same formula again and again.
Functions also make your code easier to understand. A good function name can explain what that part of the program does.
If you want to practice writing one yourself, see how to create a simple function in Python.
Common mistakes
Beginners often run into the same problems when learning functions.
Forgetting to call the function
Defining a function does not run it.
def greet():
print("Hello")
Nothing happens until you do this:
greet()
Missing parentheses when calling a function
This prints the function object, not the result:
def greet():
print("Hello")
print(greet)
You need parentheses to call it:
print(greet())
Output:
Hello
None
The Hello comes from inside the function.
The None appears because greet() does not return a value.
Confusing print() with return
If you need to use the result later, print() is not enough.
Use return instead.
If you are unsure how print() works, see Python print() function explained.
Using bad indentation inside the function
Python uses indentation to know which lines belong to the function.
Incorrect indentation will cause an error or change how your code behaves.
Trying to use a variable from inside the function outside of it
This does not work:
def make_message():
message = "Hello"
make_message()
print(message)
message only exists inside the function.
A simple fix is to return it:
def make_message():
message = "Hello"
return message
text = make_message()
print(text)
Simple debugging checks
These commands can help you understand what is happening:
print(my_function)
print(my_function())
help(print)
type(my_function)
What they do:
print(my_function)shows thatmy_functionis a functionprint(my_function())runs the function and prints its return valuehelp(print)shows help for the built-inprint()functiontype(my_function)shows the object type
If a function call gives an argument error, you may also need how to fix TypeError: missing required positional argument.
FAQ
What is a function in Python?
A function is a named block of code that runs when you call it.
Why should I use functions?
Functions help you reuse code, reduce repetition, and keep programs organized.
What is the difference between print and return?
print shows a value on the screen. return sends a value back from the function.
Can a function run without parameters?
Yes. Some functions take no input values.
What happens if I do not use return?
The function returns None by default.