beginner

Understanding Return Values

Learn how return statements work in Python functions.

Return values allow functions to send data back to the caller. Understanding how return works is essential for writing useful functions that produce results.

📚 Concepts & Theory

The return statement is how functions send data back to the code that called them. Without return, a function completes but returns None.

Basic Return:

def get_value():
return 42

result = get_value() # result = 42

Return vs Print:

def print_value():
print(42) # Displays to console
# Returns None implicitly

def return_value():
return 42 # Sends value back

# print_value() → displays 42, returns None
# return_value() → returns 42

Multiple Return Statements:

def check_positive(num):
if num > 0:
return "Positive"
elif num < 0:
return "Negative"
else:
return "Zero"

Early Return Pattern:

def validate_age(age):
if age < 0:
return "Invalid" # Exit early
if age < 18:
return "Minor"
return "Adult" # Only if previous returns didn't trigger

Returning Multiple Values:

def get_coordinates():
return 10, 20 # Returns a tuple

x, y = get_coordinates() # Tuple unpacking

Returning Different Types:

def divide(a, b):
if b == 0:
return None # Return None for error
return a / b # Return number for success

Functions Without Return:

def print_hello():
print("Hello")
# No return statement

result = print_hello() # result is None

Using Returned Values:

def square(n):
return n 2

# Direct use
print(square(5)) # 25

# Store in variable
result = square(5)

# Use in expression
total = square(3) + square(4) # 25

# Chain function calls
print(square(square(2))) # 16

Best Practices:**

  • Always return something (explicit is better than implicit)

  • Return same type consistently (or document mixed types)

  • Use None for error/missing values

  • Don't return and print the same value

🎯 Your Challenge

Write a function calculate_square(n) that returns the square of a number.

📝 Starter Code

Python
def calculate_square(n):
    pass
  • Use the return keyword to send the value back
  • Calculate the square using n ** 2 or n * n
  • The ** operator raises a number to a power
  • Don't print the result, return it
  • Make sure to actually return the calculated value

Solution

Python
def calculate_square(n):
    return n ** 2

Explanation

This function demonstrates the proper use of the return statement. The expression n ** 2 calculates the square (n raised to power 2), and return sends this value back to the caller. When you call calculate_square(5), Python evaluates 5 ** 2 (which equals 25) and returns that value. You can then store it in a variable, print it, or use it in further calculations. The ** operator is Python's exponentiation operator. Alternatively, you could use n * n for the same result.

⚠️ Common Mistakes to Avoid

  • Using print() instead of return
  • Forgetting the return statement entirely
  • Thinking return and print are the same
  • Not understanding that return exits the function
  • Trying to use the result of a function that doesn't return anything

❓ Frequently Asked Questions

return sends a value back to the caller (you can use it). print displays to console (returns None).
Yes! But only one executes per function call. The first return that runs exits the function immediately.
The function returns None by default, which can cause unexpected bugs.
Yes! return a, b, c returns a tuple that can be unpacked: x, y, z = function()

🔗 Related Exercises