Return a result
Quick review (2 minutes)
A shop has a price board on the wall and a clerk with a calculator.
A return line works out its expression, ends the call and gives that value back to the calling line; a return with no expression gives back None. A call whose body runs to its end without reaching a return line gives back None, and that includes a body that prints its result.
Decide what value the rest of the program needs from the function.
Write
returnfollowed by that value, or by the expression that works it out, at each point where the function's work is done. In the first worked example that is the last line of the body.At the call, do something with the value: give it a name, print it or use it in an expression.
Trace a call by replacing it with the value it returns, then finishing the line the call was in.
If a function should only display something, leave
returnout and call it on a line of its own.
Printing inside a function is not returning. A function whose body prints its result and has no return line gives back None. A line such as total = show_double(5) + 1 then displays 10 and stops with a TypeError when Python tries to add None and 1. A TypeError is the error for an operation applied to a value of the wrong type. Use return when the calling line needs the value.
The opposite slip is to return a value and drop it. A line that holds only total_price(4, 3) runs the function, and in a file that is run it displays nothing. Python writes out the value of such a line only when the line is typed in the Shell and the value is not None.
1 In plain words
A shop has a price board on the wall and a clerk with a calculator. The board shows a number to anyone who looks. The clerk works out a number and hands it to you on a slip, and you can add it to your own sums.
print is the board: it displays a value for a person to read. A return line is the clerk. It ends the call and hands a value back to the line that made the call. That value is the function's return value.
A call that returns a value can stand wherever a value can. The program can give it a name, print it or use it in a calculation.
A function that reaches the end of its body without a return line still gives something back: the value None, which stands for nothing.
Remember one sentence: print shows a value, and return hands it back. To look it up, see section 7.6 of the Python 3.13 language reference, on the return statement.
2 Picture it
A table of two calls. The call show_double(5) displays 10 and gives back None. The call give_double(5) displays nothing and gives back 10. The row for give_double is marked.
3 Why
When Python reaches a return line it works out the expression after the word return, leaves the function at once and puts that value where the call was. A return with nothing after it gives back None. In bill = total_price(4, 3) the call becomes 12, and the name bill is tied to 12.
Because return leaves the function, no later line of the body runs in that call. A function can hold more than one return line, and the first one Python reaches ends the call.
A call that finishes normally gives some value back. When the body has no return line, that value is None.
4 The rule
A return line works out its expression, ends the call and gives that value back to the calling line; a return with no expression gives back None.
A call whose body runs to its end without reaching a return line gives back None, and that includes a body that prints its result.
5 How
- Decide what value the rest of the program needs from the function.
- Write
returnfollowed by that value, or by the expression that works it out, at each point where the function's work is done. In the first worked example that is the last line of the body. - At the call, do something with the value: give it a name, print it or use it in an expression.
- Trace a call by replacing it with the value it returns, then finishing the line the call was in.
- If a function should only display something, leave
returnout and call it on a line of its own.
6 Worked example
Use a returned price twice
def total_price(price, count):
return price * count
bill = total_price(4, 3)
print(bill)
print(total_price(2, 5) + 1)
Standard input
Empty
Standard output
12
11
Predict the two output lines first. Then save the code as price.py and run the file, or follow the trace below. Each call is replaced by the value it returns.
Print in one function and return from another
def show_double(n):
print(n * 2)
def give_double(n):
return n * 2
result = show_double(5)
print(result)
print(give_double(5))
Standard input
Empty
Standard output
10
None
10
Predict the three output lines first. Then save the code as double.py and run the file. The body of show_double displays 10. It has no return line, so the call gives back None, and that is what the second output line shows. give_double displays nothing itself. It returns 10, and the last line prints that value.
Leave a function at the first return
def late_fee(days):
if days == 0:
return 0
return 2
print(late_fee(0))
print(late_fee(4))
Standard input
Empty
Standard output
0
2
Predict the two output lines first. Then save the code as fee.py and run the file. For 0 days the condition is True and the first return line ends the call, so the second is never reached. For 4 days the condition is False, the indented block is skipped and the call ends at the second return line.
Trace
| Instruction | Call becomes | Output | Explanation |
|---|---|---|---|
def total_price(price, count): | — | — | The def line stores the body. |
bill = total_price(4, 3) | 12 | — | The call ties price to 4 and count to 3. The return line gives back 4 * 3. bill now names 12. |
print(bill) | — | 12 | The value the function returned is displayed. |
print(total_price(2, 5) + 1) | 10 | 11 | The call is worked out first and gives back 10. Then 10 + 1 is worked out and displayed. |
7 The trap
Printing inside a function is not returning. A function whose body prints its result and has no return line gives back None. A line such as total = show_double(5) + 1 then displays 10 and stops with a TypeError when Python tries to add None and 1. A TypeError is the error for an operation applied to a value of the wrong type. Use return when the calling line needs the value.
The opposite slip is to return a value and drop it. A line that holds only total_price(4, 3) runs the function, and in a file that is run it displays nothing. Python writes out the value of such a line only when the line is typed in the Shell and the value is not None.
Quick checks
Quick check 1 of 2
Predict the two lines this program displays. The program reads no input.
def area(width, height):
return width * height
print(area(3, 4))
print(area(2, 2) + area(1, 5))Show the steps and the answer
It displays 12 and then 9.
Why: The first call returns 3 * 4. In the second line the calls return 4 and 5, and 4 + 5 is 9.
Source: Python Software Foundation, Python 3.13 Language Reference, 7.6 The return statement
Quick check 2 of 2
What does this program display, and why is the second line not a number?
def show_sum(a, b):
print(a + b)
answer = show_sum(2, 3)
print(answer)Show the steps and the answer
It displays 5 and then None.
Why: The body prints 5. It has no return line, so the call gives back None, and answer names None.
Source: Python Software Foundation, Python 3.13 Tutorial, 4.8 Defining Functions
Sources
- Python 3.13 Language Reference, 7.6 The return statement. Python Software Foundation. Python Software Foundation License Version 2. Read 1 Dec 2026.
- Python 3.13 Language Reference, 6.3.4 Calls. Python Software Foundation. Python Software Foundation License Version 2. Read 1 Dec 2026.
- Python 3.13 Tutorial, 4.8 Defining Functions. Python Software Foundation. Python Software Foundation License Version 2. Read 1 Dec 2026.
- Python 3.13 Language Reference, 8.1 The if statement. Python Software Foundation. Python Software Foundation License Version 2. Read 1 Dec 2026.
- Python 3.13 Library Reference, Built-in Exceptions: TypeError. Python Software Foundation. Python Software Foundation License Version 2. Read 1 Dec 2026.
- Python 3.13 Language Reference, 7.1 Expression statements. Python Software Foundation. Python Software Foundation License Version 2. Read 1 Dec 2026.