Skip to content
PythonFunctionsbeginner18 min read

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

Return a calculated result
python
def add(a, b):
return a + b
 
result = add(2, 5)
print(result)
Output
7

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

Printing is not the same as returning
python
def print_square(n):
print(n * n)
 
def return_square(n):
return n * n
 
first = print_square(3)
second = return_square(3)
 
print('First result:', first)
print('Second result:', second)
Output
9 First result: None Second result: 9

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

A function that finishes without executing an explicit return statement returns None. A bare return statement, written without an expression, also returns None. None is a Python value that represents the absence of a value.

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

Statements after return do not execute
python
def check_number(n):
return n * 2
print('This line will not run')
 
result = check_number(5)
print(result)
Output
10

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

Use a return value in another calculation
python
def add(a, b):
return a + b
 
subtotal = add(4, 6)
total = subtotal * 2
 
print(subtotal)
print(total)
Output
10 20

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

Python evaluates function-call arguments before entering the called function. In add_five(double(3)), double(3) is evaluated first, and its returned value is then passed to add_five().

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

A function with no explicit return
python
def announce():
print('Task complete')
 
result = announce()
print(result)
Output
Task complete None

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

Do not write return and expect the program to display the returned value automatically. To display it, use print(function_name()) or store the result and print the variable. Also remember that statements after a return are unreachable along that execution path.

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.

Incorrect Approach
script.py
python
Using print() when the caller needs a reusable result.
Correct Solution
script.py
python
Use return to send the calculated value back to the caller. The caller can then store, print, or process it.

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.

Incorrect Approach
script.py
python
Assuming return automatically displays its value.
Correct Solution
script.py
python
return supplies a value to the caller; use print() separately when you want to display that value.

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.

Incorrect Approach
script.py
python
Assuming a function that only prints a number returns that number.
Correct Solution
script.py
python
A function with no explicit return statement returns None when it finishes, even if it displayed 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.

Incorrect Approach
script.py
python
Writing important statements after return and expecting them to execute.
Correct Solution
script.py
python
Move statements that must run before the return statement. Once return executes, the current function call ends.

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.

Incorrect Approach
script.py
python
Confusing the function's displayed output with its returned value.
Correct Solution
script.py
python
Trace both separately. A print() call displays text, while the function's return value is what the caller receives.

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.

Incorrect Approach
script.py
python
Ignoring the value returned by a function.
Correct Solution
script.py
python
Assign the returned value to a variable or use the function call in another expression when the result is needed.

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.

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?
Answer:
A return value is the value a function sends back to its caller through a return statement. The caller can store it in a variable, print it, pass it to another function, or use it in an expression.
Q2:What is the difference between print() and return?
Answer:
print() displays information to the output stream, whereas return sends a value back to the caller and ends the current function call. A function can print a number without returning that number. If it finishes without an explicit return statement, it returns None.
Q3:What happens when a function executes return?
Answer:
Python evaluates the expression after return, if one is present, supplies that value to the caller, and ends the current function call. Statements later in the function body are not executed along that path.
Q4:What does a function return when it has no return statement?
Answer:
If the function reaches its end without executing an explicit return statement, it returns None. The same None result is produced by a bare return statement with no expression.
Q5:Can a returned value be used in another calculation?
Answer:
Yes. For example, if double(4) returns 8, then double(4) + 2 evaluates to 10. Python evaluates the function call and uses its returned value in the larger expression.
Q6:Can a function have more than one return statement?
Answer:
Yes. A function can contain multiple return statements, often in different branches. The return statement reached during a particular call ends that call and supplies its result.
Q7:Does return automatically display a result?
Answer:
No. return supplies the value to the caller but does not automatically display it. To display the value, use print(function_name()) or store the result and pass the variable to print().
Q8:Why are return values useful when organizing a program?
Answer:
They let functions produce results independently of how those results will be used. This supports reuse, makes calculations easier to combine, and separates producing a value from displaying it.

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.
Was this lesson helpful?
Found an error? Report an issue

Related Topics & Next Steps

4 Recommended Lessons

Strengthen your Python foundation with these complementary core lessons: