Return Values
The return statement serves two purposes: it breaks out of the function (stops execution), and it optionally sends a value back to the code that called the function.
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 Return Does
When you call a function, the program jumps into that function's code, executes its statements, and then needs to know when to stop and go back to where it was called. The return statement is how a function tells the program: I am done, exit me now, and here is what I computed. Without a return statement, a function runs until it reaches the end of its code block. With a return statement, the function stops immediately, no matter where it appears in the code.
The return statement serves two purposes: it breaks out of the function (stops execution), and it optionally sends a value back to the code that called the function.
How Execution Flows When Return Happens
To understand return values deeply, you need to see what happens inside a function when a return statement executes. The function does not continue to the next line. Instead, control immediately jumps back to the line that called the function, carrying the returned value with it.
Notice that once return is reached, any code after it in the function is never executed. The function stops completely and hands control back to the caller. This is what we mean by break out of the function.
Returning a Value to the Caller
A return statement can include a value. That value is computed inside the function and then delivered back to the code that called the function. The caller can then use that value—store it in a variable, pass it to another function, print it, or use it in a calculation.
Finding the Maximum of Two Numbers
Write a function that takes two numbers and returns the larger one.
- 1Define the function
Create a function called maximum that accepts two parameters, a and b.
- 2Compare the values
Use an if statement to check if a is greater than b.
- 3Return the larger value
If a > b, return a. Otherwise, return b. This is the value that goes back to the caller.
- 4Call the function and use the result
When you call
maximum(3, 5), the function computes and returns 5. You can store this in a variable like result =maximum(3, 5), and result now holds 5.
The function returns the maximum value, and the caller receives it and can use it immediately.
Return Without a Value
A return statement does not have to include a value. You can write return by itself, with nothing after it. When you do, the function still exits immediately, but it returns a special value called None. None is Python's way of representing nothingness or the absence of a value.
A return statement without a value is equivalent to return None. If a function reaches its end without an explicit return statement, it also returns None automatically.
This is useful when a function's job is to perform an action (like printing, modifying data, or writing to a file) rather than compute and deliver a value. The function still returns, but the caller knows not to expect a meaningful result.
Common Mistakes with Return Statements
Common mistake Forgetting that return stops the function immediately
What it looks likeWriting code after a return statement and expecting it to run
Why it failsOnce return executes, the function exits. Any lines after return in that function are never reached.
Do this insteadPlace return at the end of the function, or structure your logic so that return is called only when you are truly done with the function.
Common mistake Confusing return with print
What it looks likeUsing print(value) inside a function and expecting the caller to receive that value
Why it failsprint sends output to the screen but does not send a value back to the caller. The function still returns None unless you explicitly return a value.
Do this insteadUse return value to send a value back to the caller. Use print only if you want to display output on the screen.
Common mistake Returning a value but not using it
What it looks likeCalling maximum(3, 5) without storing or using the result
Why it failsThe function computes and returns 5, but if you do not capture it in a variable or use it, it is lost.
Do this insteadStore the result in a variable: result = maximum(3, 5), or use it immediately: print(maximum(3, 5)).
Common mistake Not understanding that return None is implicit
What it looks likeWriting a function that does not have an explicit return statement and being surprised it returns None
Why it failsEvery function returns something. If there is no explicit return, Python returns None automatically.
Do this insteadBe aware that functions without a return statement return None. If you want a value returned, add an explicit return statement.
When to Use Return Values
Use return when your function computes a value that the caller needs. For example, a function that calculates the maximum of two numbers, computes a sum, or validates input should return the result. This makes your function reusable and composable—the caller can use the returned value in further calculations or decisions.
Use return without a value (or let the function end naturally, returning None) when your function performs an action but does not need to deliver a result. For example, a function that prints a greeting, writes data to a file, or updates a data structure might not return a meaningful value.
Practice
Write a function called is_positive that takes a single number as a parameter. The function should return True if the number is greater than zero, and False otherwise. Then call your function with the values 5, -3, and 0, and print the results. What does your function return for each input?
Hints
- Use an if statement to check if the number is greater than zero.
- Return True or False based on the condition.
- Call the function three times with different values and print each result.
Summary
- 1The return statement exits a function immediately and optionally sends a value back to the caller.
- 2When a function executes return value, that value is delivered to the code that called the function and can be stored in a variable or used directly.
- 3A return statement without a value (or no return statement at all) returns None, Python's representation of nothingness.
- 4Use return when your function computes a value the caller needs; use return without a value when the function performs an action but does not need to deliver a result.
- 5Code after a return statement in a function is never executed—return stops the function completely.
At a glance
- The return statement breaks out of a function and optionally sends a computed value back to the caller.
- Returned values are immediately available to the code that called the function and can be stored or used in further operations.
- A return statement without a value returns None, which is useful for functions that perform actions rather than compute results.
- Any code after a return statement in a function is unreachable and will never execute.
- Design functions to return values when they compute something useful, making them reusable and composable.