
Python
Learn how functions send results back to your code using return statements.
Understanding the difference between `print()` and `return` is fundamental to writing effective functions. The `print()` function displays output on the screen but doesn't send any data back to the caller, making it useful only for side effects. The `return` statement, on the other hand, actually sends data back through the assignment, allowing that value to be captured, stored, and used further in your program.
# print() — displays on screen, doesn't send data back
def add_print(a, b):
print(a + b)
add_print(5, 3) # Prints: 8
# return — sends data back to calling code
def add_return(a, b):
return a + b
result = add_return(5, 3) # result = 8
print(result) # Can use the data!def multiply(a, b):
return a * b
result = multiply(4, 5)
print(result) # Output: 20def calculate_total(price, quantity):
total = price * quantity
return total
order1 = calculate_total(10, 3) # 30
order2 = calculate_total(20, 2) # 40
print(f"Order 1: ${order1}")
print(f"Order 2: ${order2}")
print(f"Total: ${order1 + order2}")Output:
Order 1: $30
Order 2: $40
Total: $70One of the powerful features of functions that return values is the ability to chain them together—using the output of one function as the input to another. This creates elegant, composable code where each function does one thing and returns a result that feeds into the next operation. Function chaining is a core technique in functional programming and leads to more readable, maintainable code.
def add(a, b):
return a + b
def multiply(a, b):
return a * b
# Use the return value from add() as input to multiply()
result = multiply(add(2, 3), 4) # (2+3) * 4 = 20
print(result)def celsius_to_fahrenheit(celsius):
fahrenheit = (celsius * 9/5) + 32
return fahrenheit
temp_c = 0
temp_f = celsius_to_fahrenheit(temp_c)
print(f"{temp_c}°C = {temp_f}°F") # 0°C = 32.0°F
temp_c = 100
temp_f = celsius_to_fahrenheit(temp_c)
print(f"{temp_c}°C = {temp_f}°F") # 100°C = 212.0°Fdef calculate_grade(score):
if score >= 90:
return "A"
elif score >= 80:
return "B"
elif score >= 70:
return "C"
else:
return "F"
print(calculate_grade(95)) # A
print(calculate_grade(85)) # B
print(calculate_grade(72)) # CWhen a `return` statement is executed, the function immediately terminates and returns the value to the caller—no further code in the function will execute. This is important because it allows you to exit early from a function when you have your answer, avoiding unnecessary computation. Understanding this behavior helps you write more efficient code and prevents bugs where code you thought would run actually never executes.
def check_age(age):
if age >= 18:
return "You are an adult"
print("Checking age...") # This won't print if age >= 18
return "You are a minor"
print(check_age(25)) # Output: You are an adult
print(check_age(15)) # Output: Checking age... You are a minorSometimes you need a function to return more than one value—for example, both the minimum and maximum of a list. Python makes this easy because returning multiple comma-separated values automatically creates a tuple, which you can then unpack into individual variables. This is more elegant than returning a dictionary or list when you have a fixed number of known return values.
def get_min_max(numbers):
return min(numbers), max(numbers)
nums = [5, 2, 8, 1, 9]
min_val, max_val = get_min_max(nums)
print(f"Min: {min_val}, Max: {max_val}") # Min: 1, Max: 9def get_person_info(name, age):
return {
"name": name,
"age": age,
"birth_year": 2024 - age
}
person = get_person_info("Alice", 25)
print(person["name"]) # Alice
print(person["birth_year"]) # 1999Some functions don't return values (they just do something):
def say_hello():
print("Hello!") # No return statement
say_hello()
# What gets stored?
result = say_hello()
print(result) # Output: None# Print function — shows data but doesn't return it
def process_print(x):
print(x * 2)
data = process_print(5) # Prints: 10
print(data) # Prints: None (no return!)
# Return function — sends data back
def process_return(x):
return x * 2
data = process_return(5)
print(data) # Prints: 10 (we have the value!)1. Use return for results — Not print
2. Name variables clearly — `result = function()`
3. Return consistent types — Don't return string sometimes, int other times
4. Document return values — Tell users what you return
def calculate_tax(amount, rate):
"""
Calculate tax amount.
Returns:
float: The tax amount
"""
return amount * rate| Concept | Remember |
|---|---|
| return | Sends data back from function |
| Only displays on screen | |
| Storing | Use `variable = function()` |
| Chaining | Use returned values as inputs |
| Exit | return stops function execution |
| None | Functions without return give None |
Now that functions can take data IN (parameters) and send data OUT (return), let's learn about scope — where variables exist and can be accessed.
Practice: Try challenges with return statements
Resources
Ojasa Mirai
Master AI-powered development skills through structured learning, real projects, and verified credentials. Whether you're upskilling your team or launching your career, we deliver the skills companies actually need.
Learn Deep • Build Real • Verify Skills • Launch Forward