Last modified: Feb 05, 2026 By Alexander Williams
Python Return Values: Function Output Guide
Every Python function gives something back. This is its return value. It is the core of function communication.
Understanding return values is key. It makes your code predictable and reusable. This guide explains everything you need to know.
What is a Return Value?
A return value is the result a function sends back to the code that called it. It is the function's output.
You use the return statement to send this value. Once Python hits a return statement, the function stops running.
It immediately exits and passes the value back.
The Basic Return Statement
Let's start with a simple example. This function calculates the square of a number.
def square(number):
"""Return the square of the input number."""
result = number * number
return result # This sends the value back
# Calling the function and using its return value
my_number = 5
squared_value = square(my_number)
print(f"The square of {my_number} is {squared_value}.")
The square of 5 is 25.
The function square computes a value. The return result line sends it out.
The calling code catches this value in the variable squared_value. You can then print it or use it further.
For a solid foundation, review our Python Function Syntax Guide for Beginners.
Returning Multiple Values
Python functions can return more than one item. They do this by packing values into a tuple.
You can then unpack them into separate variables. This is very powerful.
def get_user_info():
"""Return multiple values as a tuple."""
name = "Alice"
age = 30
city = "London"
return name, age, city # This is a tuple: (name, age, city)
# Unpacking the returned tuple
user_name, user_age, user_city = get_user_info()
print(f"Name: {user_name}, Age: {user_age}, City: {user_city}")
Name: Alice, Age: 30, City: London
The line return name, age, city creates a tuple. The calling line unpacks it into three variables.
This technique is cleaner than using global variables. It keeps your data flow clear.
This relates to the concept of Python Function Argument Unpacking.
The Implicit Return: None
What if a function has no return statement? It still returns something. It returns None.
None is a special constant in Python. It means "nothing" or "no value here".
def greet(name):
"""This function prints but does not explicitly return a value."""
print(f"Hello, {name}!")
# Call the function
result = greet("Bob")
print(f"The return value of greet() is: {result}")
print(f"Is it None? {result is None}")
Hello, Bob!
The return value of greet() is: None
Is it None? True
The greet function prints a message. It does not have a return statement.
So, Python automatically returns None. We store this None in the variable result.
Always be aware of this. A function that doesn't return a useful value returns None.
Early Returns and Control Flow
You can use multiple return statements in one function. The first one that runs ends the function.
This is useful for checks or conditional logic. It makes code simpler.
def check_positive(number):
"""Return a message based on the number's sign."""
if number > 0:
return "Positive Number" # Function stops here if true
elif number < 0:
return "Negative Number" # Function stops here if true
else:
return "It's Zero" # Function stops here
print(check_positive(10))
print(check_positive(-5))
print(check_positive(0))
Positive Number
Negative Number
It's Zero
Each condition has its own return. The function exits as soon as one condition is met.
This pattern avoids deep nesting with else statements. It leads to flatter, more readable code.
Returning Complex Data Structures
Functions can return any Python object. This includes lists, dictionaries, and even other functions.
Returning a dictionary is common for structured data.
def create_report(sales, expenses):
"""Calculate and return a financial report as a dictionary."""
profit = sales - expenses
margin = (profit / sales) * 100 if sales > 0 else 0
return {
"total_sales": sales,
"total_expenses": expenses,
"net_profit": profit,
"profit_margin": round(margin, 2)
}
report = create_report(10000, 6500)
print(f"Profit: ${report['net_profit']}")
print(f"Full Report: {report}")
Profit: $3500
Full Report: {'total_sales': 10000, 'total_expenses': 6500, 'net_profit': 3500, 'profit_margin': 35.0}
The function returns a dictionary. This bundles related data into one neat package.
The calling code can easily access any piece of data using its key, like report['net_profit'].
Common Pitfalls and Best Practices
Here are key points to remember about return values.
1. A function call is an expression. Its value is whatever it returns. You can use it directly.
# Using a function call directly in an expression
total = 10 + square(3) # square(3) returns 9, so total becomes 19
print(total)
19
2. Be clear about your return type. A function should consistently return the same type of data.
Mixing return types (like sometimes a string, sometimes a list) confuses users of your function.
3. Document the return value. Use docstrings to state what the function returns.
This is crucial for others reading your code, or for your future self.
For a deeper dive into how functions are built and called, see Python Function Parts and Calls Explained.</