Function Basics and Return Statements
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 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.
Define the function: Create a function named maximum that accepts two parameters, a and b.
Compare the values: Use an if statement to check if a is greater than b. If true, the condition is met; otherwise, b is larger.
Return the result: If a is greater, return a. Otherwise, return b. The return statement immediately exits the function with the chosen value.
Call 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
Forgetting that code after return does not execute
Once a return statement is encountered, the function stops executing immediately. Any code written after the return is unreachable.
Fix:
Place all necessary code before the return statement, or use multiple return statements in different branches of an if-else structure.Confusing the return value with printing
print() displays output to the screen but does not create a return value. The calling code cannot capture or use a printed value.
Fix:
Use return to send a value back to the caller. Use print only when you want to display output.Not capturing the return value
If you do not assign the return value to a variable or use it immediately, it disappears and you cannot access it later.
Fix:
Store the result: result = maximum(5, 3) or use it directly: print(maximum(5, 3)).Assuming a function without an explicit return returns nothing
Every function returns something. If there is no explicit return statement, Python automatically returns None.
Fix:
Understand 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
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
- The return statement exits a function immediately and optionally sends a value back to the caller.
- Any code written after a return statement in a function does not execute because the function has already exited.
- Functions can return values that the calling code can capture and use, or they can return None if no explicit return statement is provided.
- Every function implicitly returns None at the end unless you write your own return statement.
- Return statements are useful for exiting a function early when a condition is met, avoiding unnecessary computation.
Key Takeaways
- 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.