Return Values and the Return Statement
Think of return as a control-flow action and a value-transfer action. It tells the function to stop, then tells the caller what result came back, if a result was provided.
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…
The Function Call Has a Destination
A function call does not have to be the end of a computation. A function can perform work, produce a result, and send that result back to the code that called it. The return statement performs both parts of this job: it ends the current function execution and optionally passes a value back to the caller.
Think of return as a control-flow action and a value-transfer action. It tells the function to stop, then tells the caller what result came back, if a result was provided.
Tracing the Stop Point
When a function is called, Python enters that function and executes its statements in order. The moment Python encounters return, it stops executing the rest of that function body. Control immediately passes back to the line that called the function. Any statements written after that return in the same function do not execute during that call.
Following a return through a function
A function performs two steps, reaches a return statement, and has another statement after return. Which parts execute?
- 1Enter
The call transfers execution into the function, and Python begins with the first statement.
- 2Execute in order
Statements before return execute in their normal order.
- 3Reach return
The return statement is encountered, so the function stops immediately.
- 4Skip the remainder
Statements after return in that function body do not execute for this call.
- 5Resume at the caller
Control goes back to the code that called the function.
Only statements before and including the return action take part in this call; the caller resumes after the function call.
What do you think happens?
A function reaches return and then has two more statements in its body. What happens to those two statements during that call?
Reveal answer
They do not execute for that call.
Once return is reached, no further statements in that function execute. Control immediately passes back to the caller.
The Value Crossing the Boundary
A return value is the value sent from a function back to the code that called it. The value after return, if one is present, becomes the result of that function call.
The caller can use a returned value in several ways. It can assign the value to a variable, pass it to another function, print it, or use it as part of an expression. This makes return the bridge between a function's internal computation and the rest of the program.
Imagine a function that takes two numbers and returns the larger one. The comparison happens inside the function. The larger number then crosses back to the caller, where the caller can store it, print it, pass it onward, or use it in another expression.
The function does not keep the returned value isolated from the rest of the program. Once returned, the caller receives it as the result of the function call and decides how to use it.
Choosing Which Return Runs
A function can contain multiple return statements. However, only one return executes during any single call. Which one runs depends on which condition is met during that call. As soon as one return is reached, the function exits, so the other return statements are not reached for that call.
A function with alternative results
A function compares two numbers and has one return for the larger first number and another return for the larger second number. What happens in one call?
- 1Receive inputs
The function begins with the two numbers supplied by its caller.
- 2Evaluate the condition
The function determines which comparison result applies to those inputs.
- 3Reach one return
The return associated with the met condition sends the larger number back.
- 4Stop immediately
The function exits at that return, so the alternative return is not executed during the same call.
One call produces one returned result through one executed return statement.
Mistakes Beginners Make
Common mistake Assuming statements after return still execute
What it looks likeA function reaches return and is expected to perform another action later in its body.
Why it failsOnce return is reached, no further statements in that function execute for that call.
Do this insteadTrace the function in order and mark return as the point where execution leaves the function.
Common mistake Treating return as only a way to stop
What it looks likeThe caller expects a computation from the function, but the returned value is ignored conceptually.
Why it failsReturn can both exit the function and send a value back to the caller.
Do this insteadAsk two questions: where does control go next, and what value, if any, reaches the caller?
Common mistake Expecting every function to return a meaningful value
What it looks likeA function has no explicit returned value, but the caller is assumed to receive a computed result.
Why it failsFunctions that do not explicitly return a value return None by default.
Do this insteadCheck whether a return statement supplies a value before deciding what the caller receives.
Common mistake Thinking every return statement runs
What it looks likeA function has multiple possible returns, and all of them are treated as part of one call.
Why it failsOnly one return executes per call, depending on which condition is met.
Do this insteadIdentify the first return reached along the execution path and stop tracing that function there.
Practice the Execution Trace
Describe the execution flow for a function that receives two numbers, compares them, and returns the larger one. Include what happens inside the function, when execution stops, where control goes, and how the caller can use the returned number.
Hints
- Start at the function call and move through the function's statements in order.
- Identify the condition that determines which return statement is reached.
- State what happens to statements after the selected return.
- Finish by naming at least two ways the caller can use the returned value.
Practice answer
Trace a call to a function that compares two numbers and returns the larger one.
- 1Call
The caller invokes the function with two numbers, so execution enters the function.
- 2Compare
The function evaluates which number is larger.
- 3Return
The return statement associated with the met condition sends the larger number back.
- 4Exit
The function stops immediately at return; no later statement in that function executes for this call.
- 5Use
The caller can assign, pass, print, or use the returned number in an expression.
The larger number becomes the result of the function call and is available to the caller.
A Reliable Mental Model
- 1Use this four-part trace whenever you read a function with return: enter the function, execute statements in order, stop immediately when return is reached, and resume at the caller with the returned value if one was supplied. If no value is explicitly returned, the caller receives None by default.
- Return exits the current function.
- Statements after the reached return do not execute during that call.
- A value after return becomes the result of the function call.
- The caller can use a returned value in several ways.
- Only one return executes per call, and a function without an explicit returned value returns None by default.
Key Takeaways
- The return statement immediately exits a function and transfers control back to its caller.
- A value written after return is sent back as the result of the function call.
- The caller can assign, pass, print, or use the returned value in an expression.
- Only one return executes during a single call, even when a function contains multiple return statements.
- A function that does not explicitly return a value returns None by default.