Function Return Values
The return statement is used to return from a function i.e. break out of the function. We can optionally return a value from the function as well.
What a Return Statement Does
When you call a function, the computer executes the code inside it line by line. But what happens when the function finishes? And how does a value computed inside the function get back to the code that called it? The answer is the return statement. A return statement does two things at once: it stops the function from running any more code, and it sends a value back to whoever called the function.
The return statement is used to return from a function—that is, to break out of the function and stop executing any remaining code inside it. You can optionally return a value from the function as well. When a return statement runs, control immediately passes back to the caller, and any code after the return statement in that function does not execute.
Return Statement Execution Flow
To understand return statements deeply, it helps to trace what happens step by step. When a function contains a return statement, that statement acts as an exit point. The moment the return statement is reached, the function stops—no code below it runs, even if there are more lines in the function body.
In the flowchart above, notice that when the condition is true and the return statement executes, the function exits immediately. Line 3 never runs. This is the key behavior of return: it is an exit point, not just a statement that happens to pass a value back.
Returning a Value vs. Returning Nothing
A return statement can take two forms. You can write return by itself, which exits the function and sends back a special value called None. Or you can write return followed by an expression, which exits the function and sends back the value of that expression.
| Form | Syntax | What It Does | Value Sent Back |
|---|---|---|---|
| Return with no value | return | Exits the function immediately | None |
| Return with a value | return someValue | Exits the function and sends back the value of someValue | The result of evaluating someValue |
Both forms exit the function. The difference is what the caller receives. If you use return by itself, the caller gets None. If you use return with a value, the caller gets that value. Most of the time, when you write a function to compute something, you will use return with a value so that the result can be used by the code that called the function.
How a Returned Value Reaches the Caller
When you call a function and assign its result to a variable, the returned value flows back to that assignment. The function executes, computes a result, and returns it. The caller then receives that value and can use it in any expression.
The sequence above shows the journey of a returned value. The caller invokes the function. The function runs, computes a result, and executes a return statement with that result. The value then flows back to the caller, where it is assigned to a variable or used in an expression. This is how functions deliver their computed results to the rest of your program.
Worked Example: A Function That Returns a Maximum
Finding the Maximum of Two Numbers
Write a function that takes two numbers as parameters and returns the larger one. Then call it and use the returned value.
Define the function with two parameters: The function needs to accept two values to compare. Call them a and b.
Use an if statement to compare them: Check if a is greater than b. If so, we will return a. Otherwise, we will return b.
Return the larger value: In the if branch, return a. In the else branch, return b. Either way, the function exits and sends back the larger number.
Call the function and capture the result: When you call the function with two numbers, the returned value is assigned to a variable. You can then use that variable in your program.
The function returns the maximum of the two parameters. When called with find_max(7, 3), it returns 7. When called with find_max(2, 9), it returns 9.
Multiple Return Statements and Early Exit
A function can have more than one return statement. Each return statement is a potential exit point. When the function runs, it executes code line by line until it hits a return statement. That return statement then exits the function immediately. If the function has multiple return statements in different branches (for example, in different if/else blocks), only the one that is actually reached during execution will run.
This is useful for writing functions that need to exit early under certain conditions. For example, a function might check if an input is invalid and return an error value immediately, without running the rest of the function body. Or a function might search for something and return as soon as it finds it, rather than continuing to search after a match is found.
What do you think happens?
Suppose a function has three return statements in three different if/else branches. When you call the function, how many of those return statements actually execute?
Reveal answer
Answer: Exactly one, depending on which condition is true
Only the return statement in the branch that is actually executed will run. As soon as any return statement executes, the function exits. The other return statements are never reached during that particular function call.
Returning Multiple Values
Sometimes you want a function to return more than one value. You might think a function can only return a single value, but there is a way around this: use a tuple. A tuple is a collection of values grouped together. When you return a tuple, you are returning multiple values at once. The caller receives the tuple and can unpack it into separate variables.
For example, a function might compute both the sum and the product of two numbers. Instead of writing two separate functions, you can write one function that returns both results as a tuple. The caller then unpacks the tuple to get both values.
Returning a tuple is a common pattern when you need to return multiple related values from a function. The tuple groups the values together, and the caller can unpack them into separate variables for use.
Common Mistakes with Return Statements
Forgetting to return a value when the function should compute and deliver a result
The caller expects a number but receives None, which cannot be used in arithmetic or other operations intended for the computed result.
Fix:
Always include a return statement with the value you want to send back when the function's purpose is to compute and deliver a result.Writing code after a return statement that you expect to run
Once a return statement executes, the function exits immediately. Any code after the return statement in that function will never run.
Fix:
Remember that return is an exit point. If you have code that must run before the function exits, place it before the return statement.Confusing return with print
print sends output to the screen but does not send a value back to the caller. The function still returns None, not the printed value.
Fix:
Use return to send a value back to the caller. Use print only if you want to display output to the user.Assuming all return statements in a function will execute
Only the return statement in the branch that is actually executed will run. The function exits as soon as the first return statement is reached.
Fix:
Understand that return exits the function immediately. Only one return statement per function call will execute.
Where the Returned Value Ends Up
When you call a function, the returned value can be used in several ways. Most commonly, you assign it to a variable. But you can also use the returned value directly in an expression, pass it to another function, or use it in a comparison.
In the diagram above, you can see the journey of a returned value. The function is called, computes a result, returns it, and that value is then assigned to the variable x. After this assignment, x holds the returned value and can be used anywhere in your program.
Practice: Writing Functions with Return Statements
Write a function called is_even that takes a single number as a parameter and returns True if the number is even, and False if the number is odd. Then call the function with the number 7 and use the returned value in an if statement to print either 'The number is even' or 'The number is odd'.
Hints
- Use the modulo operator (%) to check if a number is divisible by 2.
- If number % 2 equals 0, the number is even. Otherwise, it is odd.
- Your function should have two return statements: one that returns True and one that returns False.
- After calling the function and storing the result, use that result in an if statement.
Write a function called get_min_max that takes three numbers as parameters and returns both the minimum and the maximum as a tuple. Then call the function with the numbers 5, 12, and 8, unpack the returned tuple into two variables, and print both values.
Hints
- A tuple is created by grouping values with commas and parentheses, like (a, b).
- You can return a tuple by writing return (min_value, max_value).
- To unpack a tuple into two variables, write min_val, max_val = get_min_max(5, 12, 8).
- Use the built-in min() and max() functions to find the minimum and maximum of the three numbers.
Summary
- A return statement exits a function immediately and optionally sends a value back to the caller.
- return by itself sends back None; return value sends back the result of evaluating value.
- When a return statement executes, any code after it in that function does not run.
- A function can have multiple return statements in different branches, but only the one that is actually reached will execute.
- The returned value can be assigned to a variable, used in an expression, or passed to another function.
- You can return multiple values by returning a tuple, which the caller can then unpack into separate variables.
Key Takeaways
- The return statement exits a function and optionally sends a value back to the caller.
- Once a return statement executes, the function stops immediately—no code after it runs.
- A function can have multiple return statements, but only the one that is reached during execution will run.
- Returned values can be assigned to variables, used in expressions, or passed to other functions.
- Multiple values can be returned as a tuple, which the caller can unpack into separate variables.