Sub topic 3
Sending results back with return
This chapter assumes you can define a function with def, call it with arguments, and write simple if statements. It covers how return hands a value to the caller. It also covers what happens when a function has no return, which is a common source of bugs.
What return does
A return statement ends the function and sends a value back to the code that called it. The call expression then evaluates to that value, so you can store it, print it, or use it inside a larger expression.
def square(n):
return n * n
total = square(4) + square(3)
print(total) # 25Each call to square is replaced by its returned value (16 and 9) before the addition happens.
return ends the function immediately
When Python runs a return, the function stops. Any code after it on that path does not run. You can use this to exit early, which often removes the need for nested if and else blocks.
def sign(n):
if n < 0:
return "negative"
if n == 0:
return "zero"
return "positive"
print(sign(-3)) # negative
print(sign(0)) # zero
print(sign(8)) # positiveOnly one return runs per call. The caller gets the value from whichever one was reached first.
The implicit None
If a function finishes without reaching a return that has a value, Python returns None for you. None is the built-in value that means "no value here". Its type is NoneType.
def show(message):
print(message)
result = show("hi")
print(result) # NoneThe first line of output, hi, comes from the print inside show. The second, None, is what show returned. Printing a value and returning it are different actions. print writes text to the screen, and the caller cannot use that text. return gives the caller a value it can keep working with.
Common errors
The most frequent mistake is writing print where you meant return. The function appears to work when you call it on its own, but the result is None when you try to use it.
def add_wrong(a, b):
print(a + b)
total = add_wrong(2, 3) + 10 # prints 5, then raises TypeErrorAdding None and an integer raises a TypeError. The fix is to change print(a + b) to return a + b.
A second mistake is a return that only some paths reach. Here the loop returns when it finds a match, but nothing returns when it does not.
def first_even(values):
for v in values:
if v % 2 == 0:
return v
print(first_even([1, 3, 4])) # 4
print(first_even([1, 3])) # NoneThe second call returns None because the loop ends without a match. That can be a reasonable design if callers check for it, as in if first_even(data) is None:. It becomes a bug when the caller assumes the result is always a number.
A third mistake is putting a statement after return and expecting it to run. Python skips it without any warning.
Choosing what to return
A function that computes something should usually return it and leave printing to the caller. This keeps the function reusable. The same square can feed a calculation, a test, or a report without changing its code.
Exercise
What does this code print, and why? def label(n): if n > 0: return "positive" print(label(5)) print(label(-5))
Show answer
It prints "positive" and then None. The first call reaches the return. For the second call the condition is false, so the function ends without a return and Python returns None. Adding a final return "not positive" would give the second call a string.