Return Values
The return statement sends a result from a function back to the code that called it. Returning values, instead of just printing them, lets you store results, combine functions and build bigger programs from small, testable pieces.
Returning a value
When Python reaches return, the function stops immediately and the call is replaced by the returned value. You can store it, print it or use it in a calculation.
def add_vat(price):
return price * 116 / 100
phone = add_vat(1000)
print(phone)
print(add_vat(250) + add_vat(750))
(The result is a float because / always gives a float, as explained on Numbers & Arithmetic.)
return vs. print
This is the most important distinction on this page. print() shows a value on the screen for a human to read. return hands a value back to your program so it can keep working with it.
def show_double(n):
print(n * 2)
def double(n):
return n * 2
a = show_double(5)
b = double(5)
print("a =", a)
print("b =", b)
show_double(5) printed 10 but returned nothing, so a is None. double(5) printed nothing but returned 10, which we stored in b.
Functions without return give None
If a function ends without a return statement, or uses return on its own, it returns None automatically.
Forgetting to return and then using the result. If double() printed instead of returning, double(5) + 1 would fail with TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'. Whenever you see NoneType in an error, check for a missing return.
Returning early
Because return ends the function immediately, you can use it to finish as soon as you know the answer. This often makes code flatter and clearer than a long if/elif/else.
def grade(marks):
if marks >= 80:
return "A"
if marks >= 65:
return "B"
if marks >= 50:
return "C"
return "D"
print(grade(91), grade(70), grade(42))
An early return also exits any loops inside the function, which solves the "break out of nested loops" problem from Nested Loops:
def find_first(grid, target):
for r, row in enumerate(grid):
for c, value in enumerate(row):
if value == target:
return r, c # leaves both loops at once
return None # only reached if nothing was found
seats = [[3, 8], [6, 0]]
print(find_first(seats, 0))
print(find_first(seats, 9))
Any code placed after a return in the same block never runs. Editors such as VS Code often grey it out as "unreachable code".
Returning several values
To return more than one value, separate them with commas. Python packs them into a tuple, which you can unpack straight into variables.
def min_max(numbers):
return min(numbers), max(numbers)
low, high = min_max([34, 12, 56, 7, 41])
print(low, high)
result = min_max([3, 9])
print(result)
Functions that answer yes or no
A function can return a Boolean, which reads beautifully inside an if. Such functions are often named is_... or has_....
def is_valid_phone(number):
return len(number) == 10 and number.startswith(("07", "01"))
print(is_valid_phone("0712345678"))
print(is_valid_phone("712345678"))
print(is_valid_phone("0112345678"))
if not is_valid_phone("12345"):
print("Please enter a 10-digit number starting with 07 or 01")
startswith() accepts a tuple of options and returns True if the string starts with any of them. Notice the function returns the condition directly; there's no need for if ...: return True else: return False.
Combining functions
Because returned values can be passed straight into other functions, you can build complex behaviour from simple parts.
def double(n):
return n * 2
def add_vat(price):
return price * 116 / 100
print(add_vat(double(500)))
Python works from the inside out: double(500) returns 1000, then add_vat(1000) returns 1160.0.
Key points
return valueends the function and sends the value back to the caller.print()shows a value to the user;returngives it to your program.- A function with no
returnreturnsNone. - Return early to keep code simple;
returnalso exits loops inside the function. return a, breturns a tuple, which you can unpack withx, y = f().
Tip: use the ← and → keys to move between pages.
