Return Values

Concepts covered: Functions

Functions can send data back to the caller using the return statement. This is how a function hands a result back for the caller to store or pass on to another function.

def calculate_area(width, height):
    return width * height

# Store the returned value in a variable
area1 = calculate_area(5, 3)
area2 = calculate_area(10, 4)

print("First area: " + str(area1))
print("Second area: " + str(area2))
print("Combined area: " + str(area1 + area2))

# Use directly in expressions
total_cost = calculate_area(12, 8) * 25
print("Cost at $25 per sq unit: $" + str(total_cost))

Output:

First area: 15
Second area: 40
Combined area: 55
Cost at $25 per sq unit: $2400

The return statement immediately exits the function and sends the specified value back to where the function was called. That value replaces the function call in the expression, as if you had written the value directly.

Return versus print

print() and return get confused because both look like output, but only one of them hands the calling code a value it can use:

print()
  • Displays text on screen
  • For human consumption only
  • Function returns None
  • Can't use the value later
  • Side effect, not a result
return
  • Sends value back to caller
  • For program consumption
  • Value can be stored and used
  • Enables further calculations
  • Actual function result
def add_with_print(a, b):
    # Shows the sum but returns nothing
    print(a + b)

def add_with_return(a, b):
    # Returns the sum for further use
    return a + b

# Try to use the results
result1 = add_with_print(3, 5)
result2 = add_with_return(3, 5)

print("result1 is: " + str(result1))
print("result2 is: " + str(result2))
print("result2 doubled: " + str(result2 * 2))

Output:

8
result1 is: None
result2 is: 8
result2 doubled: 16

add_with_print displays 8 on the screen but returns None, so result1 is useless. add_with_return does not display anything but returns 8, which we can store and reuse in later calculations.

Functions without a return

If a function doesn't have a return statement, or if return is used without a value, the function automatically returns None. None is Python's special value representing "nothing" or "no value."

def say_hello():
    print("Hello!")
    # No return statement

def say_goodbye():
    print("Goodbye!")
    return

result1 = say_hello()
result2 = say_goodbye()

print("Result 1:", result1)
print("Result 2:", result2)
print("Both are None:", result1 is None and result2 is None)

Output:

Hello!
Goodbye!
Result 1: None
Result 2: None
Both are None: True

Functions without return values exist for their "side effects", such as printing messages or writing to files. Return a value when the caller needs one back; leave it out when the action itself is the function's job.

Return exits immediately

When Python encounters a return statement, it immediately exits the function. Any code after the return statement never runs. This is useful for early exits when you have already determined the result:

def check_age(age):
    if age < 0:
        return "Invalid: age cannot be negative"
    if age < 18:
        return "Minor"
    if age < 65:
        return "Adult"
    return "Senior"

print(check_age(-5))
print(check_age(15))
print(check_age(30))
print(check_age(70))

Output:

Invalid: age cannot be negative
Minor
Adult
Senior

Each condition checks the age and returns as soon as one matches, so the function never runs more code than it needs to.

About this lesson

This section is part of the Functional Programming: Beginner lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.