Functions That Return Values
Understand how Python functions return values: the difference between return and print, capturing results in variables, returning to a caller, and combining function results. Beginner-friendly with runnable code and a quiz.
Key takeaways
- return sends a value back from a function so the rest of your program can use it
- print shows text on screen; return hands a value back — they do completely different jobs
- You capture a returned value by storing it in a variable: answer = double(5)
- A function with no return statement gives back the special value None
- Because functions return values, you can combine their results, like add(a, b) inside another sum
The difference that changes everything
You've probably written functions that print things. But the most powerful functions don't just show a result — they hand one back to the rest of your program so it can be used again. That handing-back is done with one small keyword: return. Once it clicks, your functions become building blocks you can snap together.
This lesson builds on knowing how to define a function. If you've seen python functions and parameters, you're in a great spot. Let's focus on what return really does.
print shows, return gives
These two functions look similar but behave completely differently:
def double_print(n):
print(n * 2) # shows the answer
def double_return(n):
return n * 2 # hands the answer back
Watch what happens when we try to use their results:
a = double_print(5) # screen shows: 10
b = double_return(5) # screen shows nothing
print(a) # None
print(b) # 10
double_print(5) prints 10 to the screen — but it gives nothing back, so a becomes None. double_return(5) prints nothing, yet it returns 10, which we capture in b.
The rule: print is for humans to read; return is for your program to use. A printed value is gone the moment it appears. A returned value can be stored, calculated with, and passed around.
Capturing the value
When a function returns something, you usually catch it in a variable:
def square(n):
return n * n
result = square(6)
print(result) # 36
print(result + 4) # 40
square(6) works out 36 and hands it back; result = square(6) stores it. Now result is a normal variable — we can print it, add to it, or use it anywhere a number fits. That reusability is the whole point of returning values.
You can also use the returned value directly, without a variable:
print(square(3)) # 9
print(square(3) + square(4)) # 9 + 16 = 25
return ends the function
The instant return runs, the function stops and goes back to the caller. Any lines after it are skipped:
def check(score):
if score >= 50:
return "Pass"
return "Fail"
print(check(72)) # Pass
print(check(30)) # Fail
When score is 72, return "Pass" runs and the function ends right there — it never reaches return "Fail". This is a clean, common pattern: return as soon as you know the answer.
No return means None
If a function never hits a return, Python quietly returns the special value None, which means "nothing here":
def greet(name):
print(f"Hi, {name}!")
x = greet("Sam") # prints: Hi, Sam!
print(x) # None
greet only prints; it has no return, so x is None. That's perfectly fine for a function whose job is just to do something. The key is knowing which kind of function you're writing: an action (returns None) or a calculation (returns a value).
Combining returned values
Because functions return values, their results can feed into one another — this is where the magic happens:
def add(a, b):
return a + b
def average(a, b):
total = add(a, b) # use one function's result...
return total / 2 # ...inside another
print(average(10, 20)) # 15.0
average calls add to get the total, then divides by 2. Each function does one small job and returns its result, and bigger functions assemble those results into bigger answers. That's exactly how large programs are built — from small, returning pieces.
Worked example: a grade reporter
Let's combine everything into a tidy program. Each function does one job and returns a value; a final action function ties them together.
def average(numbers):
"""Return the mean of a list of numbers."""
return sum(numbers) / len(numbers)
def to_grade(score):
"""Return a letter grade for a score."""
if score >= 90:
return "A"
elif score >= 75:
return "B"
elif score >= 60:
return "C"
else:
return "F"
def report(name, scores):
"""Print a report. This is an action, so it returns None."""
avg = average(scores)
grade = to_grade(avg)
print(f"{name}: average {avg:.1f}, grade {grade}")
# Use the returning functions
report("Amara", [88, 92, 95])
report("Ben", [60, 55, 70])
# We can also use the returned values directly
top = max(average([88, 92, 95]), average([60, 55, 70]))
print(f"Best average: {top:.1f}")
How it works:
averagereturns the mean. It calculates and hands the number back, so it can be reused anywhere.to_gradereturns a letter. It uses earlyreturns — the moment it finds the right band, it stops.reportis an action: it calls the two returning functions, then prints. It has noreturn, so it gives backNone— and that's correct for a function whose job is to display.- The last two lines prove the power of returns: we feed two
average(...)results straight intomax(...)to find the best, with no intermediate variables at all.
Notice how the returning functions are the reusable building blocks, while the action function arranges them into output.
Try it yourself
Write a set of cooperating, returning functions:
is_even(n)that returnsTrueifnis even andFalseotherwise (hint:n % 2 == 0).bigger(a, b)that returns whichever ofaorbis larger — without using the built-inmax.celsius_to_fahrenheit(c)that returnsc * 9 / 5 + 32.- Then write one action function
show(c)that callscelsius_to_fahrenheitand prints a sentence like"20°C is 68.0°F". Notice it has no return — it's an action.
When your returning functions are solid, see how they slot into a real project in building a calculator in Python, where every operation is a function that returns its answer.
Quick quiz
Test yourself and earn XP
What does the return statement do?
return hands a value back to the caller, so it can be stored or used further. It does not print anything.
What does this print? ``` def triple(n): return n * 3 print(triple(4)) ```
triple(4) returns 4 * 3 = 12, which print then displays.
What does a function return if it has NO return statement?
A function with no return automatically gives back the special value None.
Why can't you do maths with a function that only prints?
print shows text but returns None, so result = print(...) captures nothing useful. You need return to get a value.
What happens to code written AFTER a return that runs?
Once return runs, the function stops immediately, so any later lines in it are skipped.
FAQ
Yes. Write return a, b and Python bundles them into a tuple. The caller can unpack them like x, y = get_point(). This is handy when a function naturally produces a pair, such as a minimum and a maximum, or a quotient and a remainder. Under the hood you are still returning one object — a tuple — but it feels like several values.
No. Some functions exist purely to do an action, like printing a menu or saving a file, and those naturally return None. Functions that calculate or fetch a value should return it. A good habit is to decide up front: is this function an action (do something) or a calculation (give something back)? That choice tells you whether you need return.
Keep exploring
More in Coding