Return Values
Understand how return sends results from functions to callers, how returned values differ from printed output, and how to reuse function results in expressions.
In this lesson, you will learn
- ✓Explain the difference between returning a value and printing it.
- ✓Describe how return ends the current function call.
- ✓Store a returned value in a variable and use it in another expression.
- ✓Trace function execution and predict exact output.
- ✓Explain what a function returns when it reaches the end without an explicit return statement.
- ✓Write and test functions that return calculated results.
1. What does return do?
The return statement sends a value from a function back to the code that called it. When Python executes return, the current function call ends immediately. If an expression follows return, Python evaluates that expression and supplies its value to the caller.
Consider a function that adds two numbers. It can calculate a + b and return the result. The caller can then decide what to do with that result: print it, store it in a variable, compare it with another number, or pass it to another function.
A function definition does not execute its body by itself. The body runs when the function is called. If the body reaches a return statement, the function exits at that point and control moves back to the caller.
Return a calculated result
The definition creates a function named add with two parameters, a and b. The call add(2, 5) supplies 2 for a and 5 for b. The expression a + b evaluates to 7. return sends 7 back to the caller and ends that function call. The assignment stores the returned value in result. print(result) displays 7.
2. The difference between return and print()
print() displays information. It is useful when you want a person to see a message or result in the program's output.
return sends a value back to the caller. That value can be saved and reused by other statements. A function can return a value without printing anything, and a function can print information without explicitly returning a useful result.
One important detail is that print() itself returns None. Therefore, if a function only calls print() and has no other return statement, the function's result is None. The text displayed by print() is not automatically the function's return value.
The following example shows both operations separately so you can see that displaying a number and returning a number are different behaviors.
Printing is not the same as returning
print_square(3) calculates 9 and displays it. Because the function does not explicitly return a value, its result is None. The assignment first = print_square(3) stores that None result in first, not the displayed number 9. return_square(3) calculates 9 and returns it without displaying it by itself. The assignment stores the returned 9 in second. The final two print() calls show that first is None while second is 9. The first output line is produced inside print_square. The remaining lines are produced by the two final print() calls.
Note
3. Return ends the function immediately
When Python executes a return statement, it leaves the current function call. Statements later in the same function body are not executed during that call.
This behavior is useful when a function has finished its task or when it needs to stop after handling a particular condition. In this lesson, focus on the basic rule: once return executes, the function exits and supplies its result to the caller.
If a function contains multiple possible return statements, only the return statement reached during that particular execution supplies the result. The function may behave differently for different inputs.
Statements after return do not execute
The function is called with n equal to 5. The expression n * 2 evaluates to 10. return sends 10 back to the caller and immediately ends the function call. The print() statement after return is never executed during this call. The assignment stores 10 in result, and the final print() displays 10.
4. Store and combine returned values
Returned values can be used wherever an expression of the appropriate type is expected. You can assign a result to a variable, use it in arithmetic, compare it with another value, or pass it as an argument to another function.
This makes functions useful as building blocks. One function can calculate a result, and another operation can use that result without needing to know how the first function performed its calculation.
In the next example, add() returns a number. The caller stores that number and uses it in a second calculation.
Use a return value in another calculation
add(4, 6) returns 10. The assignment stores 10 in subtotal. The expression subtotal * 2 evaluates to 20, which is stored in total. The first print() displays 10, and the second displays 20. The add() function does not need to know how its result will be used. Its responsibility is to return the sum.
Note
5. Practical use and edge cases
Use return when a function's caller needs a result for further processing. For example, a calculation function can return a number that is later printed, compared, or included in a larger calculation.
A function that only displays a message may appropriately use print(). A function that calculates a value for the rest of the program to use will often return it instead. The correct choice depends on the function's intended responsibility.
Remember that a function may return values such as integers, strings, lists, or other Python objects. The caller should use the result in a way that is compatible with its type.
Check the path taken through the function. A function may have several return statements, but once one is executed, that call ends. If execution reaches the end without a return statement, Python returns None.
Do not assume that returning a value also prints it. If you want to see the result in the console, explicitly call print() on the returned value or print it inside the function.
A function with no explicit return
Calling announce() executes its body and displays Task complete. The function does not contain an explicit return statement. When the function reaches the end, it returns None automatically. The assignment stores None in result. The final print() displays None.
Caution
Common pitfalls to avoid
Real debugging lessons and exact syntax contrasts to avoid runtime bugs
#1 Using print() when the caller needs a reusable result.
Why it happens: print() sends text to the output stream, while return passes a result back to the caller so it can be stored or used in another expression.
Key takeaway: Use return to send the calculated value back to the caller. The caller can then store, print, or process it.
#2 Assuming return automatically displays its value.
Why it happens: return exits the function and gives its value to the caller; displaying that value requires print() or another output mechanism.
Key takeaway: return supplies a value to the caller; use print() separately when you want to display that value.
#3 Assuming a function that only prints a number returns that number.
Why it happens: A function that reaches the end without an explicit return statement returns None, even if it printed a number.
Key takeaway: A function with no explicit return statement returns None when it finishes, even if it displayed a number.
#4 Writing important statements after return and expecting them to execute.
Why it happens: Once return executes, the function exits immediately, so later statements in that path are unreachable.
Key takeaway: Move statements that must run before the return statement. Once return executes, the current function call ends.
#5 Confusing the function's displayed output with its returned value.
Why it happens: Displayed output is a side effect; a returned value is data the caller receives, and these are separate behaviors.
Key takeaway: Trace both separately. A print() call displays text, while the function's return value is what the caller receives.
#6 Ignoring the value returned by a function.
Why it happens: If a caller needs the result for further calculations or decisions, capture or use the returned value rather than discarding it.
Key takeaway: Assign the returned value to a variable or use the function call in another expression when the result is needed.
Ready for hands-on challenges?
Test your skills with shuffled coding challenges in the Practice Arena.
Technical and placement interview questions
Frequently tested interview questions with detailed, full answers (click to expand)
Q1:What is a return value in Python?
Q2:What is the difference between print() and return?
Q3:What happens when a function executes return?
Q4:What does a function return when it has no return statement?
Q5:Can a returned value be used in another calculation?
Q6:Can a function have more than one return statement?
Q7:Does return automatically display a result?
Q8:Why are return values useful when organizing a program?
Key takeaways
- return sends a value to the caller and ends the current function call.
- print() displays information; it does not replace return.
- A function can return a value that the caller stores, prints, compares, or uses in another expression.
- A function that finishes without an explicit return statement returns None.
- A bare return statement also returns None.
- Statements after a return statement are not executed along that function call's return path.
- Returned values allow small functions to be combined into larger calculations.
Related Topics & Next Steps
4 Recommended LessonsStrengthen your Python foundation with these complementary core lessons: