Return Statements and 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 program jumps into that function and executes its code line by line. But what happens when the function finishes? How does the program know to stop running the function's code and go back to where it was called? The answer is the return statement. The return statement does two things at once: it breaks out of the function (stops executing any more code inside it), and it sends a value back to the place where the function was called. Without a return statement, your function still exits when it reaches the end, but it sends back a special value called None, which means nothingness.
What do you think happens?
If you write a function that prints a message but never uses the return statement, what happens when you call it? Does the function keep running forever, or does it stop at some point?
Reveal answer
Answer: The function stops when it reaches the end and returns None
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 don't explicitly write return, the function will still exit and send back None to the caller.
How Return Values Flow Back to the Caller
When a return statement executes, it does not just disappear. The value you return travels back through the call and becomes the result of that function call. Think of it like this: when you call a function, you are asking it a question. The return statement is the function's answer. That answer can then be stored in a variable, printed, used in a calculation, or passed to another function. The key insight is that a function call can be used anywhere you would use a value.
Return Stops Execution Immediately
One of the most important things to understand about the return statement is that it stops the function right away. Any code after the return statement inside that function will never run. This is called early exit. This is useful when you want to stop processing and send back a result without running the rest of the function. For example, if you are searching for a value in a list and you find it, you can return immediately without checking the rest of the list.
When a return statement executes, the function exits immediately. No code after the return statement will run, even if it is on the same line or in the same block.
Returning a Value Versus Returning Nothing
You can write a return statement in two ways. You can write return followed by a value, like return 42 or return name. This sends that value back to the caller. Or you can write return with nothing after it, like just return by itself. When you do this, the function still exits, but it returns None instead of a specific value. In fact, if you do not write any return statement at all, Python automatically adds an invisible return None at the very end of your function. This is why every function in Python always returns something, even if you did not explicitly write a return statement.
| Function Type | Return Statement | What Gets Returned | Example Use |
|---|---|---|---|
| Function that returns a value | return 42 | The number 42 | result = add(3, 4) stores 7 in result |
| Function that returns nothing explicitly | return | None | print_message() runs but does not produce a value to store |
| Function with no return statement | (implicit return None) | None | greet() still returns None even though you did not write return |
Worked Example: Finding the Maximum
A Function That Returns the Larger of Two Numbers
Write a function called maximum that takes two numbers as parameters and returns the larger one. Trace through what happens when you call maximum(5, 8).
Define the function with two parameters: The function maximum takes two parameters, a and b. These will hold the two numbers we want to compare.
Compare the two numbers with an if statement: We check if a is greater than b. If it is, we know a is the larger number.
Return the larger number: If a is greater, we execute return a, which exits the function and sends the value of a back to the caller. The function stops here.
Handle the else case: If a is not greater than b, we reach the else block and execute return b, which sends the value of b back to the caller.
The returned value becomes the result of the function call: When we call maximum(5, 8), the function compares 5 and 8, finds that 8 is larger, and returns 8. This means the expression maximum(5, 8) evaluates to 8, so result = maximum(5, 8) stores 8 in the variable result.
The function returns 8, and result holds the value 8.
8Common Mistakes with Return Statements
Forgetting to actually use the returned value
The function returns 8, but if you do not store it in a variable or print it, the value is lost and you cannot use it
Fix:
Store the result in a variable (result = maximum(5, 8)) or print it directly (print(maximum(5, 8)))Writing code after a return statement inside the function
The return statement exits the function immediately, so the print statement will never run
Fix:
Put any code that needs to run before the return statement, or remove the code after return if it is not neededConfusing return with print
print() displays a value on the screen but does not send it back to the caller. The function still returns None
Fix:
Use return to send a value back to the caller; use print only if you want to display something on the screenAssuming a function without an explicit return statement returns 0
Functions without an explicit return statement return None, not 0
Fix:
Remember that every function implicitly returns None unless you write your own return statement
Multiple Return Statements in One Function
A function can have more than one return statement. This is common when you have different conditions and want to return different values depending on which condition is true. When the function runs, it will execute the code line by line until it hits a return statement. At that moment, it exits and sends back the value from that specific return statement. The other return statements are never reached. This is why it is safe to have multiple return statements in different branches of an if/else structure.
BOnly one return statement will ever execute in a single function call. Once a return statement runs, the function exits immediately, and none of the other return statements are checked or executed.
Practice: Trace the Execution
Read this function and predict what will be printed. Then trace through the execution step by step. def mystery(x): if x < 0: return "negative" if x == 0: return "zero" return "positive" result = mystery(-5) print(result) What does this function return when called with -5? Which return statement executes? Why do the other return statements not run?
Hints
- Start at the top of the function and trace line by line
- When you reach a return statement, the function exits immediately
- Check which condition is true for x = -5
Write a function called is_even that takes one number as a parameter and returns True if the number is even, or False if it is odd. Your function should use a return statement to send back the boolean value. Test your function by calling it with a few different numbers.
Hints
- A number is even if it divides evenly by 2 (use the modulo operator %)
- You need two return statements, one for the even case and one for the odd case
- Remember to actually call your function and print or store the result
Key Takeaways
- The return statement exits a function immediately and sends a value back to the caller. Any code after the return statement does not run.
- A function can return a specific value (like return 42) or return nothing explicitly (just return), which sends back None.
- Every function in Python implicitly returns None at the end if you do not write your own return statement.
- The value returned by a function becomes the result of the function call and can be stored in a variable, printed, or used in calculations.
- A function can have multiple return statements in different branches (if/else blocks). Only one will execute per function call, and it exits the function immediately.
Key Takeaways
- The return statement breaks out of a function and sends a value back to the caller
- Functions always return something: either an explicit value you specify, or None if no return statement is present
- When a return statement executes, the function exits immediately and no code after it runs
- The returned value becomes the result of the function call and can be used wherever a value is expected
- Multiple return statements in different branches are safe because only one will execute per function call