Return Values

Functions

Return Values

In this tutorial, we'll send useful results back from functions.

Some functions print results. Other functions return results.

Returning means sending a value back to the place where the function was called.

def add(a, b):
    return a + b

result = add(3, 4)
print(result)

Output:

7

print vs return

print() shows something on the screen:

def show_total(price, quantity):
    print(price * quantity)

return gives a value back so it can be reused:

def calculate_total(price, quantity):
    return price * quantity

total = calculate_total(5, 3)
tax = total * 0.2
print(tax)

If you need the result later, return it.

return Stops the Function

When Python reaches return, the function ends:

def check_age(age):
    if age >= 18:
        return "adult"
    return "child"

If age >= 18 is true, Python returns "adult" and does not continue to the final line.

Returning Booleans

Functions often return True or False:

def is_even(number):
    return number % 2 == 0

print(is_even(10))
print(is_even(7))

Common Mistake

If a function does not return anything, Python returns None automatically:

def greet():
    print("Hello")

result = greet()
print(result)

This prints None after the greeting.

Practice

Create a function called discount_price(price) that returns the price after a 10% discount.