Return Statements and Return Values
When a return statement executes, the function exits immediately. No code after the return statement will run, even if it is on the same line or in the same block.
Try it: Defining and Calling Functions
What def and a call really do: def only creates a function object, a call binds the arguments to the parameters in a new frame and runs the body, return ends the call at once and its value replaces the call expression in the caller — while a function that only prints gives back None.
How it works
- def name(params): creates a function object and binds the name to it; the body does not run yet.
- A call evaluates its arguments, opens a new frame, binds each parameter to its argument and runs the body from the top.
- return value ends the call immediately (lines after it never run) and sends the value back; the frame and its local names are discarded.
- In the caller, the call expression is replaced by the returned value, which can be stored, used in a larger expression or passed to another call.
- A body that ends without return (e.g. one that only prints) makes the call evaluate to None — so x = f() stores None and None + 1 raises TypeError.
- Calling a name before its def has run raises NameError: the function does not exist yet.
Default run (18 steps): The program is ready. Python runs it from the top, one statement at a time. Idea: the value a call returns is what the caller stores. … The program finished. Console: 8.
Simplified: Four fixed, hand-written programs; you edit the literal arguments, switch the body between return and print, and move the def after the calls. The lab's own small evaluator runs them (your input is never executed as code); every variant was run in CPython 3.12 and the lab reproduces its sys.settrace line/call/return/exception events, its output and its error message. Functions are shown as <function name> without a memory address.
Loading the simulation…
What a Return Statement Does
When you call a function, the program jumps into that function and executes its code line by line. But what happens when the function finishes? How does the program know to stop running the function's code and go back to where it was called? The answer is the return statement. The return statement does two things at once: it breaks out of the function (stops executing any more code inside it), and it sends a value back to the place where the function was called. Without a return statement, your function still exits when it reaches the end, but it sends back a special value called None, which means nothingness.
What do you think happens?
If you write a function that prints a message but never uses the return statement, what happens when you call it? Does the function keep running forever, or does it stop at some point?
Reveal answer
The function stops when it reaches the end and returns None
Every function in Python implicitly contains a return None statement at the end unless you have written your own return statement. This means that even if you don't explicitly write return, the function will still exit and send back None to the caller.
How Return Values Flow Back to the Caller
When a return statement executes, it does not just disappear. The value you return travels back through the call and becomes the result of that function call. Think of it like this: when you call a function, you are asking it a question. The return statement is the function's answer. That answer can then be stored in a variable, printed, used in a calculation, or passed to another function. The key insight is that a function call can be used anywhere you would use a value.
Return Stops Execution Immediately
One of the most important things to understand about the return statement is that it stops the function right away. Any code after the return statement inside that function will never run. This is called early exit. This is useful when you want to stop processing and send back a result without running the rest of the function. For example, if you are searching for a value in a list and you find it, you can return immediately without checking the rest of the list.
When a return statement executes, the function exits immediately. No code after the return statement will run, even if it is on the same line or in the same block.
Returning a Value Versus Returning Nothing
You can write a return statement in two ways. You can write return followed by a value, like return 42 or return name. This sends that value back to the caller. Or you can write return with nothing after it, like just return by itself. When you do this, the function still exits, but it returns None instead of a specific value. In fact, if you do not write any return statement at all, Python automatically adds an invisible return None at the very end of your function. This is why every function in Python always returns something, even if you did not explicitly write a return statement.
| Function Type | Return Statement | What Gets Returned | Example Use |
|---|---|---|---|
| Function that returns a value | return 42 | The number 42 | result = add(3, 4) stores 7 in result |
| Function that returns nothing explicitly | return | None | print_message() runs but does not produce a value to store |
| Function with no return statement | (implicit return None) | None | greet() still returns None even though you did not write return |
Comparison of different return behaviors
Worked Example: Finding the Maximum
A Function That Returns the Larger of Two Numbers
Write a function called maximum that takes two numbers as parameters and returns the larger one. Trace through what happens when you call maximum(5, 8).
- 1Define the function with two parameters
The function maximum takes two parameters, a and b. These will hold the two numbers we want to compare.
- 2Compare the two numbers with an if statement
We check if a is greater than b. If it is, we know a is the larger number.
- 3Return the larger number
If a is greater, we execute return a, which exits the function and sends the value of a back to the caller. The function stops here.
- 4Handle the else case
If a is not greater than b, we reach the else block and execute return b, which sends the value of b back to the caller.
- 5The returned value becomes the result of the function call
When we call
maximum(5, 8), the function compares 5 and 8, finds that 8 is larger, and returns 8. This means the expressionmaximum(5, 8)evaluates to 8, so result =maximum(5, 8)stores 8 in the variable result.
The function returns 8, and result holds the value 8.
8- Line 1-5: Define the function maximum with two parameters
- Line 2-3: If a is greater than b, return a immediately (function exits here if true)
- Line 4-5: Otherwise, return b (function exits here if the if condition was false)
- Line 7: Call
maximum(5, 8); the function compares 5 and 8, finds 8 is larger, and returns 8 - Line 8: Print the result, which is 8
Common Mistakes with Return Statements
Common mistake Forgetting to actually use the returned value
What it looks likemaximum(5, 8) is called but the result is not stored or printed, so you never see the answer
Why it failsThe function returns 8, but if you do not store it in a variable or print it, the value is lost and you cannot use it
Do this insteadStore the result in a variable (result = maximum(5, 8)) or print it directly (print(maximum(5, 8)))
Common mistake Writing code after a return statement inside the function
What it looks likereturn a followed by print(a) on the next line inside the functionWhy it failsThe return statement exits the function immediately, so the print statement will never run
Do this insteadPut any code that needs to run before the return statement, or remove the code after return if it is not needed
Common mistake Confusing return with print
What it looks likeWriting print(a) instead of return a, thinking they do the same thing
Why it failsprint()displays a value on the screen but does not send it back to the caller. The function still returns NoneDo this insteadUse return to send a value back to the caller; use print only if you want to display something on the screen
Common mistake Assuming a function without an explicit return statement returns 0
What it looks likeCalling a function that has no return statement and expecting it to give you 0
Why it failsFunctions without an explicit return statement return None, not 0
Do this insteadRemember that every function implicitly returns None unless you write your own return statement
Multiple Return Statements in One Function
A function can have more than one return statement. This is common when you have different conditions and want to return different values depending on which condition is true. When the function runs, it will execute the code line by line until it hits a return statement. At that moment, it exits and sends back the value from that specific return statement. The other return statements are never reached. This is why it is safe to have multiple return statements in different branches of an if/else structure.
B- Line 1: Define function with one parameter, score
- Line 2-3: If score is 90 or higher, return A and exit the function
- Line 4-5: If score is 80 or higher (but less than 90), return B and exit
- Line 6-7: If score is 70 or higher (but less than 80), return C and exit
- Line 8-9: Otherwise, return F and exit
- Line 11: Call
check_grade(85); since 85 is >= 80 but < 90, the second condition is true and B is returned - Line 12: Print B
Only one return statement will ever execute in a single function call. Once a return statement runs, the function exits immediately, and none of the other return statements are checked or executed.
Practice: Trace the Execution
Read this function and predict what will be printed. Then trace through the execution step by step.
def mystery(x):
if x < 0:
return "negative"
if x == 0:
return "zero"
return "positive"result = mystery(-5)
print(result)What does this function return when called with -5? Which return statement executes? Why do the other return statements not run?
Hints
- Start at the top of the function and trace line by line
- When you reach a return statement, the function exits immediately
- Check which condition is true for x = -5
Write a function called is_even that takes one number as a parameter and returns True if the number is even, or False if it is odd. Your function should use a return statement to send back the boolean value. Test your function by calling it with a few different numbers.
Hints
- A number is even if it divides evenly by 2 (use the modulo operator %)
- You need two return statements, one for the even case and one for the odd case
- Remember to actually call your function and print or store the result
Key Takeaways
- 1The return statement exits a function immediately and sends a value back to the caller. Any code after the return statement does not run.
- 2A function can return a specific value (like return 42) or return nothing explicitly (just return), which sends back None.
- 3Every function in Python implicitly returns None at the end if you do not write your own return statement.
- 4The value returned by a function becomes the result of the function call and can be stored in a variable, printed, or used in calculations.
- 5A function can have multiple return statements in different branches (if/else blocks). Only one will execute per function call, and it exits the function immediately.
At a glance
- The return statement breaks out of a function and sends a value back to the caller
- Functions always return something: either an explicit value you specify, or None if no return statement is present
- When a return statement executes, the function exits immediately and no code after it runs
- The returned value becomes the result of the function call and can be used wherever a value is expected
- Multiple return statements in different branches are safe because only one will execute per function call