return sends a value back from a function to whoever called it, and it ends the function immediately - any code after a return that runs does not execute. def add(a, b): return a + b hands a + b back to the caller. If a function has no return, or hits the end without one, it returns None. That single rule explains the most common beginner bug in Python: a function that computes the right answer, prints it, forgets to return it, and hands back None to code that expected a number.
Table of contents
- return hands a value back and stops the function
- No return means None
- print versus return, the definitive difference
- Returning multiple values
- return in loops, and bare return
- How this fits the rest of the stack
- FAQ
return hands a value back and stops the function
def add(a, b):
return a + b
result = add(2, 3) # result is 5
return does two things at once. It provides the value the function call evaluates to, and it terminates the function on the spot. Statements after the return on the taken path never run:
def check(n):
if n < 0:
return "negative"
return "non-negative" # only reached when n >= 0
Once a return executes, control leaves the function immediately. This is what makes early returns - returning as soon as you know the answer - a clean way to avoid deeply nested if blocks. You handle the edge cases up front, return out of them, and let the main path run unindented below.
No return means None
Every function returns something. If you do not write return, or the function ends without hitting one, Python returns None:
def greet(name):
print(f"Hello, {name}") # prints, but returns nothing
x = greet("Sam") # prints Hello, Sam; x is None
This is the bug behind countless why is my variable None questions. The function did its visible job - it printed - so it looks like it worked. But it did not hand anything back, so the caller gets None.
The distinction to internalize: print shows a value to a human; return gives a value to the program. They are not interchangeable. If another part of your code needs the result, the function must return it, not just print it. Printing is for people; returning is for code.
print versus return, the definitive difference
This trips up nearly everyone at some point:
def double_print(n):
print(n * 2) # a side effect; caller gets None
def double_return(n):
return n * 2 # caller gets the value
double_print(5) # shows 10, returns None
total = double_return(5) # total is 10, shows nothing
print writes to the screen and evaluates to None. return yields a value your program can store, pass on, or compute with. In an interactive shell they look similar because the shell echoes the returned value - but inside a program they behave completely differently.
The test is simple: does another line of code need this result? Then return it. Do you just want to see it while debugging? Then print. Confusing the two is why a function feels like it works in the REPL but breaks when you actually use its output.
Returning multiple values
Python lets you return several values by returning a tuple, and unpack them at the call site:
def min_max(nums):
return min(nums), max(nums) # a tuple
lo, hi = min_max([4, 1, 9, 2]) # lo=1, hi=9
There is no special multiple return - return a, b builds a tuple and the caller unpacks it. It reads as if the function genuinely returns two things, which is exactly the intent.
For more than two or three values, a named structure reads better than positional unpacking:
from collections import namedtuple
Result = namedtuple("Result", "ok value error")
def parse(s):
...
return Result(ok=True, value=42, error=None)
Now callers write r.value instead of remembering that the second element is the value. Positional tuples are fine for two related values; beyond that, name them so the call site is readable.
return in loops, and bare return
return inside a loop exits the whole function, not just the loop - which is the clean way to bail out the moment you find what you want:
def first_even(nums):
for n in nums:
if n % 2 == 0:
return n # leaves the function entirely
return None # nothing even found
That is clearer than setting a flag and breaking, because the answer leaves the function as soon as it exists.
A bare return with no value returns None and is used to exit early:
def process(data):
if not data:
return # equivalent to return None
... # main work
return on its own is a guard clause: nothing to do, leave now. It makes intent explicit and keeps the main body from being wrapped in a giant if. Both patterns - return the answer when found, return early when there is nothing to do - lean on the fact that return ends the function immediately.
How this fits the rest of the stack
A function that returns its result instead of only printing it is a function you can compose, test, and reuse - the difference between a script and something you can build on. When those functions become the handlers behind an API, whether they return the right value or a silent None is exactly the sort of thing you want to catch before it ships. The RunxBuild hosting calculator lays out the service, database, storage, and bandwidth as separate line items, and the RunxBuild dashboard is where the team watches deploys, logs, and restarts as they happen.
Useful related references:
- Private Methods in Python: There Are None, and That Is Fine
- Null in Python: There Is No null, There Is None - and That Distinction Matters
- Change Python Version: The Three Tools That Make It Stop Hurting
- Python services on RunxBuild
FAQ
What does the return statement do in Python?
It sends a value back from a function to the caller and ends the function immediately. return a + b makes the function call evaluate to a + b, and any code after the executed return does not run. Without a return, the function returns None.
Why does my function return None?
Because it has no return statement on the path it took, or it ends without one. A function that only prints its result returns None to the caller. If code needs the value, the function must return it, not just print it.
What is the difference between print and return in Python?
print writes a value to the screen and evaluates to None. return hands a value back to the program so it can be stored or reused. Use print to see something while debugging and return when another line of code needs the result.
How do I return multiple values from a Python function?
Return them separated by commas, which builds a tuple - return min(nums), max(nums) - and unpack at the call site with lo, hi = .... For more than two or three values, a namedtuple or dataclass makes the call site readable by naming each field.
Does return break out of a loop in Python?
return exits the entire function, including any loop it is inside - not just the loop. That makes it a clean way to bail out the instant you find what you are searching for, clearer than setting a flag and using break.