Returning Values from Functions
The return statement breaks out of the function and optionally passes a value back to the caller. Once a return statement is reached, no code after it in that function will execute.
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 Does a Return Statement Do?
When you call a function, the code inside it runs from top to bottom. But what happens when the function finishes? And more importantly, how does a value computed inside the function get back to the code that called it? The answer is the return statement. The return statement serves two purposes: it immediately exits the function, and it optionally sends a value back to the caller. Without a return statement, a function runs to its end and sends back a special value called None.
The return statement breaks out of the function and optionally passes a value back to the caller. Once a return statement is reached, no code after it in that function will execute.
How Return Statements Work
When a function executes a return statement, two things happen simultaneously. First, the function stops executing immediately—any code that comes after the return statement in that function is skipped. Second, the value (if any) that follows the return keyword is packaged up and sent back to the line of code that called the function. That calling code can then use, store, or display that returned value. If no value is specified after return, or if the function ends without a return statement, the function returns None, which represents the absence of a meaningful value.
Single Return Values
The simplest use of return is to send a single value back to the caller. This value can be a number, a string, a list, or any other Python object. When the caller receives it, they can assign it to a variable, use it in an expression, or pass it to another function.
Returning a Computed Value
Write a function that takes a temperature in Celsius and returns the equivalent temperature in Fahrenheit.
- 1Define the function with a parameter
Create a function
celsius_to_fahrenheitthat accepts one parameter, celsius. - 2Compute the converted value
Inside the function, calculate fahrenheit = (celsius * 9/5) + 32.
- 3Return the result
Use return fahrenheit to send the computed value back to the caller.
- 4Call the function and use the returned value
When you call
celsius_to_fahrenheit(0), the function returns 32, which you can store or use immediately.
The function exits at the return statement, sends 32 back to the caller, and any code after the return statement in the function does not execute.
32.0
77.0Returning Multiple Values
Sometimes you need a function to return more than one value. Python makes this easy by allowing you to return a tuple. A tuple is a collection of values grouped together, and when you return a tuple, the caller can unpack it into separate variables. This is a powerful way to send multiple pieces of information back from a function.
You can return two or more different values from a function by using a tuple. The caller can unpack the tuple into separate variables.
Returning Multiple Values as a Tuple
Write a function that takes a list of numbers and returns both the minimum and maximum values.
- 1Define the function
Create a function
find_min_maxthat accepts a list of numbers as a parameter. - 2Find the minimum and maximum
Use
min()andmax()to find the smallest and largest values in the list. - 3Return both values as a tuple
Use return
min_val,max_val. Python automatically packages these into a tuple. - 4Unpack the returned tuple
When calling the function, use
min_result,max_result=find_min_max(numbers)to split the tuple into two variables.
The function returns a tuple containing two values, and the caller unpacks them into separate variables for easy use.
Smallest: 1, Largest: 9
Tuple returned: (10, 20)Return vs. No Return
Not every function needs to return a value. Some functions are written to perform an action—like printing output or modifying data—without sending anything back. When a function reaches its end without a return statement, or when a return statement has no value after it, the function returns None. Understanding the difference between functions that return values and functions that do not is crucial for writing clear, predictable code.
x = 25
Type of x: <class 'int'>
Hello, Alice!
y = None
Type of y: <class 'NoneType'>Multiple Return Statements
A function can have more than one return statement. This is common when you need to return different values based on different conditions. However, only one return statement will ever execute in a single function call. As soon as any return statement is reached, the function exits immediately, and the remaining code—including any other return statements—is skipped.
child
teen
adultWhen any return statement is executed, the function stops immediately. No code after that return statement—including other return statements—will run in that function call.
Common Mistakes with Return Statements
Common mistake Forgetting to assign or use the returned value
What it looks likeresult = calculate_total(10, 20) is correct; calculate_total(10, 20) alone discards the returned value.Why it failsIf you call a function that returns a value but do not assign it to a variable or use it immediately, the value is lost and you cannot access it later.
Do this insteadAlways assign the returned value to a variable if you need to use it later, or use it directly in an expression or function call.
Common mistake Writing code after a return statement in the function body
What it looks likedef add(a, b): return a + b print('Sum calculated') # This never runsWhy it failsThe return statement exits the function immediately, so any code written after it is unreachable and will never execute.
Do this insteadPlace all necessary code before the return statement, or use conditional statements to control which return statement executes.
Common mistake Returning a value but not using it in the caller
What it looks likex = get_user_input() print('Done') # x is assigned but never usedWhy it failsWhile not an error, this wastes the returned value and suggests the function design may not match the caller's needs.
Do this insteadIf a function returns a value, use that value. If you do not need it, consider whether the function should return anything at all.
Common mistake Confusing return with print
What it looks likedef add(a, b): print(a + b) # This prints but does not returnWhy it failsprint()displays output to the screen but does not send a value back to the caller. The function returns None, not the sum.Do this insteadUse return to send a value back to the caller. Use print only if you want to display output as a side effect.
When to Return Values
Deciding whether a function should return a value depends on its purpose. If a function computes or retrieves a value that the caller needs to use, it should return that value. If a function performs an action like printing, modifying a file, or updating a data structure, it may not need to return anything. Functions that return values are often easier to test and reuse because their behavior is predictable and their results can be verified.
- Return a value when the caller needs to use the result of the function's computation.
- Return a value when you want to chain function calls together (e.g., result =
process(calculate(5))). - Return None (or nothing) when the function's purpose is to perform an action, like printing or modifying data.
- Return a tuple when you need to send multiple related values back to the caller.
- Consider returning a value instead of printing inside the function—this makes the function more flexible and testable.
Practice: Tracing Return Execution
What do you think happens?
What will this code print? Trace through the execution step by step.
Reveal answer
15
The function double() is called with 5, computes 5 * 2 = 10, and returns 10. That value is added to 5 in the caller, giving 15, which is printed.
Write a function called get_initials that takes a person's full name (as a string) and returns their initials. For example, get_initials('Alice Bob') should return 'AB'. Then call the function with at least two different names and print the results.
Hints
- Split the name string into words using .split().
- Extract the first character of each word using indexing [0].
- Use a loop or list comprehension to collect the initials.
- Join the initials together using .join() and return the result.
Summary
The return statement is a fundamental tool for writing functions that send values back to their callers. When a return statement is executed, the function exits immediately, and any value specified after return is packaged and sent back. Functions can return single values, multiple values as a tuple, or nothing at all (returning None). Understanding when and how to use return statements makes your code more modular, testable, and reusable. Remember that only one return statement executes per function call, and any code after a return statement in the function body will not run.
At a glance
- The return statement exits a function and optionally sends a value back to the caller.
- A function can return a single value, multiple values as a tuple, or nothing (None).
- Once a return statement is reached, the function stops executing immediately—no code after it runs.
- Functions with multiple return statements execute only one of them per call, determined by the conditions that precede each return.
- Use return when the caller needs the result; use functions without return when the purpose is to perform an action.