Returning Values from Functions
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 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.
Define the function with a parameter: Create a function celsius_to_fahrenheit that accepts one parameter, celsius.
Compute the converted value: Inside the function, calculate fahrenheit = (celsius * 9/5) + 32.
Return the result: Use return fahrenheit to send the computed value back to the caller.
Call 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.
Define the function: Create a function find_min_max that accepts a list of numbers as a parameter.
Find the minimum and maximum: Use min() and max() to find the smallest and largest values in the list.
Return both values as a tuple: Use return min_val, max_val. Python automatically packages these into a tuple.
Unpack 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
Forgetting to assign or use the returned value
If 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.
Fix:
Always assign the returned value to a variable if you need to use it later, or use it directly in an expression or function call.Writing code after a return statement in the function body
The return statement exits the function immediately, so any code written after it is unreachable and will never execute.
Fix:
Place all necessary code before the return statement, or use conditional statements to control which return statement executes.Returning a value but not using it in the caller
While not an error, this wastes the returned value and suggests the function design may not match the caller's needs.
Fix:
If a function returns a value, use that value. If you do not need it, consider whether the function should return anything at all.Confusing return with print
print() displays output to the screen but does not send a value back to the caller. The function returns None, not the sum.
Fix:
Use 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
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.
Key Takeaways
- 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.