Python functions: why print cannot replace return
Your function shows the right number, but your variable holds None. Follow one small calculation to understand parameters, calls and return values.
You write a function to price three notebooks. It displays 12. You save the result in a variable, inspect it, and find None. Where did the twelve go?
It never left the function as a result. print writes to the output; return hands a value back to the code that called the function. That distinction explains a common beginner stumbling block.
The function that seems to work
Run this complete example in Python 3, or paste it into the online Python editor with nothing to install:
def total(price, quantity):
print(price * quantity)
amount = total(4, 3)
print(amount)12
Nonedef creates the function without running its body. The call total(4, 3) runs it: price receives 4, and quantity receives 3. Those names are parameters; the supplied values are arguments. Indentation marks the lines belonging to the function.
The first print displays the multiplication. The function then reaches its end without a return, so Python returns None, a value representing the absence of a result. That is what gets assigned to amount.
Return the value so the caller can use it
Let the function calculate and let the caller decide what to display:
def total(price, quantity):
return price * quantity
amount = total(4, 3)
delivery = 2
print(amount)
print(amount + delivery)12
14Now twelve reaches amount. We can add delivery, compare it with a budget or store it in a list. There is no need to read anything back from the screen.
Each call accepts arguments and hands back a value. print opens a separate path to the screen. You may use both while debugging, but displaying a value does not make it the returned value.
Return also ends the call
Executing return leaves the current function call immediately. Use that to handle a special case before the main calculation:
def average(values):
if not values:
return None
return sum(values) / len(values)
print(average([6, 8, 10]))
print(average([]))8.0
NoneAn empty list has no average. Here we choose to return None; another program might raise an exception. Make that choice deliberately. Returning zero would be misleading because zero can be a valid average.
The caller should check result is None before using the result in arithmetic. Avoid if not result for this check: it also matches a legitimate average of 0.0.
Try one yourself
Write remaining(budget, spent) to return the unused budget, even when it is negative. The function should print nothing. Call it with 20 and 23, store the result and display it outside the function.
Here is the solution. Pay attention to which lines are indented:
def remaining(budget, spent):
return budget - spent
balance = remaining(20, 23)
print(balance)
print(remaining(20, 20))-3
0Also try remaining(20, 0). If you get 20 and can use it in another calculation, you have the distinction we wanted. Classes and advanced type annotations can wait.
When None shows up again
Look for a path that ends without return. A function might return inside an if but fall through for other inputs. Also check for return print(...): that returns the result of print, which is None.
Next, try returning a collection instead of a number. The list, tuple and dictionary guide helps you choose one. Keep the Python cheat sheet nearby, and consult the official functions tutorial for the language rules.