What Is a Return Value in Python?
A return value is the result a function sends back after it finishes running.
This is an important Python idea because functions often do work and then give the answer back to the code that called them. A beginner mistake is thinking that print() and return do the same thing. They do not.
Use return when a function should give a result back to your program:
def add(a, b):
return a + b
result = add(2, 3)
print(result)
Output:
5
return gives the result back to the caller. Then your code can store it, print it, or use it somewhere else.
What this term means #
- A return value is the result a function sends back after it runs.
- The return value is created with the
returnkeyword. - The calling code can save that value in a variable, print it, or use it in another expression.
If you are new to functions, see what a function is in Python.
Why return values matter #
Return values matter because they make functions useful.
- They let functions produce results.
- They make code reusable because the result can be used in different places.
- They help separate calculation from display.
For example, a function can calculate a total and return it. Then another part of your program can decide what to do with that total.
Basic example #
Here is a simple function that adds two numbers and returns the answer:
def add(a, b):
return a + b
result = add(2, 3)
print(result)
Output:
5
2 and 3 map to parameters a and b; return a + b sends 5 back to the caller.What happens here:
def add(a, b):creates a function with two inputs.return a + bsends the sum back.result = add(2, 3)stores the returned value in a variable.print(result)shows that value on the screen.
If you need a refresher on function inputs, see what an argument is in Python and what a parameter is in Python.
Return vs print #
This is one of the most common beginner confusions.
print()shows text or values on the screen.returnsends a value back from a function.- A function can return a value without printing anything.
- Visible output is not the same as a returned result.
Example using print() #
def add(a, b):
print(a + b)
result = add(2, 3)
print(result)
Output:
5
None
Why does None appear?
- The function prints
5 - But it does not return anything
- So Python returns
Noneby default - That
Nonegets stored inresult
Example using return #
def add(a, b):
return a + b
result = add(2, 3)
print(result)
Output:
5
If you want to understand print() better, see Python print() function explained.
What happens if there is no return #
If a function does not use return, Python returns None.
def greet():
print("Hello")
print(greet())
Output:
Hello
None
This surprises many beginners. The function printed "Hello", but it did not send back a useful value.
That is why functions that only print are different from functions that return data.
This also explains some NoneType errors. If your code expects a real value but gets None, you may see errors like AttributeError: ‘NoneType’ object has no attribute … or TypeError: ‘NoneType’ object is not iterable.
Using a return value #
Once a function returns a value, you can use it in different ways.
Save it in a variable #
def get_name():
return "Maya"
name = get_name()
print(name)
Output:
Maya
Use it in math or string operations #
def add(a, b):
return a + b
total = add(4, 6)
print(total * 2)
Output:
20
Pass it into another function #
def add(a, b):
return a + b
def show_number(number):
print("The number is:", number)
show_number(add(2, 3))
Output:
The number is: 5
If you want more practice with this idea, see return values in Python functions and Python functions explained.
Common beginner mistakes #
Here are some common problems with return values.
Using print() when return is needed #
Wrong:
def square(x):
print(x * x)
result = square(4)
print(result)
Output:
16
None
Better:
def square(x):
return x * x
result = square(4)
print(result)
Forgetting to capture the returned value #
A function can return a value, but if you do not store or use it, that result may be lost.
def add(a, b):
return a + b
add(2, 3) # returned value is ignored
Better:
result = add(2, 3)
print(result)
Writing code after return and expecting it to run #
When Python reaches return, the function ends immediately.
def example():
return 10
print("This will not run")
The print() line never runs.
Expecting every function to return a useful value #
Some functions return useful data, but some do not. If a function has no return, it gives back None.
Common causes of confusion #
- Confusing printed output with returned output
- Forgetting that functions without
returngive backNone - Trying to use the result of a function that only prints
- Placing important code after a
returnstatement
Helpful checks #
If you are not sure what a function returns, these quick checks can help:
print(add(2, 3))
result = add(2, 3)
print(result)
print(type(result))
You can also inspect built-in functions:
help(print)
help(len)
FAQ #
What is a return value in Python? #
It is the value a function sends back to the place where the function was called.
Is return the same as print? #
No. print() displays something. return gives a result back from the function.
What does a function return if I do not write return? #
It returns None.
Can a function return more than one value? #
Yes. Python can return multiple values, usually as a tuple.