Function Basics and Return Statements
When a return statement executes, the function stops running right then. No further lines in that function are executed after the return, even if there is more code written below it.
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 the Return Statement Does
The return statement serves two critical purposes in a function. First, it breaks out of the function—meaning execution stops immediately at that point, and any code after the return statement in that function does not run. Second, it can optionally send a value back to the place where the function was called. This returned value becomes the result of the function call itself, and the calling code can use that value however it needs.
When a return statement executes, the function stops running right then. No further lines in that function are executed after the return, even if there is more code written below it.
How Return Statements Control Program Flow
To understand what happens when a return statement runs, imagine the function as a path. When you call a function, your program jumps into that path and starts executing line by line. If a return statement is encountered, the program immediately jumps back to where the function was called, carrying the returned value with it. Any code in the function that comes after the return statement is never reached.
Functions That Return Values
When a function returns a value, that value becomes the result of calling the function. The calling code can capture this result in a variable, use it in an expression, or pass it to another function. Consider a function that finds the maximum of two numbers. It compares the two inputs and returns whichever one is larger.
Maximum Function
Write a function that takes two numbers as parameters and returns the larger one.
- 1Define the function
Create a function named maximum that accepts two parameters, a and b.
- 2Compare the values
Use an if statement to check if a is greater than b. If true, the condition is met; otherwise, b is larger.
- 3Return the result
If a is greater, return a. Otherwise, return b. The return statement immediately exits the function with the chosen value.
- 4Call the function
When you call
maximum(5, 3), the function executes, compares 5 and 3, and returns 5. The value 5 becomes the result of the function call.
The function returns 5, which can be stored in a variable, printed, or used in further calculations.
In this example, the return statement not only exits the function but also sends a value back. The calling code receives that value and can work with it immediately.
Functions Without Explicit Return Statements
Not every function needs to explicitly return a value. However, 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 do not write a return statement, the function will still return something—it will return None.
If you call a function that does not have an explicit return statement and try to print the result, you will see None printed. This is the implicit return value.
Using Return to Exit Early
The return statement is not just for sending values back to the caller. It is also a powerful tool for exiting a function before reaching the end. This is called an early return or early exit. You can use return to stop executing the function as soon as a certain condition is met, without running the rest of the code.
For example, imagine a function that validates user input. If the input is invalid, you might want to return immediately with an error message or a special value, rather than continuing to process invalid data. By returning early, you avoid unnecessary computation and make the code clearer.
Early returns make functions easier to read and understand. Instead of nesting many if-else statements, you can check for special cases at the beginning and return immediately if they apply.
How Return Values Travel Back to the Caller
When a function returns a value, that value does not simply disappear. It is passed back to the exact location where the function was called. The calling code can then use that value in any way it needs—store it in a variable, use it in a calculation, pass it to another function, or print it.
Common Mistakes with Return Statements
Common mistake Forgetting that code after return does not execute
What it looks likeA function that prints a message after a return statement will never print that message because the return exits the function first.
Why it failsOnce a return statement is encountered, the function stops executing immediately. Any code written after the return is unreachable.
Do this insteadPlace all necessary code before the return statement, or use multiple return statements in different branches of an if-else structure.
Common mistake Confusing the return value with printing
What it looks likeWriting print(value) inside a function is not the same as return value. The print happens, but the function does not send the value back to the caller.
Why it failsprint()displays output to the screen but does not create a return value. The calling code cannot capture or use a printed value.Do this insteadUse return to send a value back to the caller. Use print only when you want to display output.
Common mistake Not capturing the return value
What it looks likeCalling a function like maximum(5, 3) without storing the result in a variable means the returned value is lost.
Why it failsIf you do not assign the return value to a variable or use it immediately, it disappears and you cannot access it later.
Do this insteadStore the result: result = maximum(5, 3) or use it directly: print(maximum(5, 3)).
Common mistake Assuming a function without an explicit return returns nothing
What it looks likeA function that does not have a return statement still returns None, not nothing.
Why it failsEvery function returns something. If there is no explicit return statement, Python automatically returns None.
Do this insteadUnderstand that None is a valid return value. If you want a function to return a specific value, use an explicit return statement.
Predicting Function Behavior
What do you think happens?
A function is defined with three lines: line 1 does some calculation, line 2 returns a value, and line 3 prints a message. When you call this function, what happens to line 3?
Reveal answer
Line 3 is skipped because the return statement on line 2 exits the function
When a return statement executes, the function stops immediately. Any code after the return statement, including line 3, is never reached or executed.
Practice: Identifying Return Behavior
For each scenario below, determine whether the function will return a value and what that value will be. 1) A function that adds two numbers and uses return to send the result back. 2) A function that prints a greeting but has no return statement. 3) A function that checks if a number is positive and returns True if it is, False otherwise. 4) A function that performs a calculation but only prints the result without using return.
Hints
- Remember that every function returns something, even if there is no explicit return statement.
- Think about the difference between printing and returning.
- A return statement sends a value back to the caller;
print()only displays output. - If there is no explicit return, the function implicitly returns None.
Summary
- 1The return statement exits a function immediately and optionally sends a value back to the caller.
- 2Any code written after a return statement in a function does not execute because the function has already exited.
- 3Functions can return values that the calling code can capture and use, or they can return None if no explicit return statement is provided.
- 4Every function implicitly returns None at the end unless you write your own return statement.
- 5Return statements are useful for exiting a function early when a condition is met, avoiding unnecessary computation.
At a glance
- The return statement breaks out of a function and optionally sends a value back to the caller.
- Code after a return statement does not execute; the function stops immediately when return is encountered.
- Functions without an explicit return statement implicitly return None.
- Return values can be captured in variables or used directly by the calling code.
- Early returns are a best practice for exiting a function when a condition is met.