Velamuvi AIAcademy

Lesson 23 of 30 · Functions

Return Values

Understand return in Python. Learn the difference between printing and returning, how to use a function's result and how to return more than one value.

  • 6 min read
  • Beginner
  • Reviewed
In this lesson you will learn
  • How a function hands a result back with return
  • The crucial difference between print and return
  • What a function gives back when it has no return
  • How to return more than one value

Getting an answer back

Some functions do something, such as printing. Others work something out and hand you the answer. len("hello") does not print 5. It gives 5 back to you, so you can store it, compare it or use it in a calculation.

The return statement is how your own functions hand back a result.

Try it yourselfPython
def add(a, b):
    return a + b

result = add(3, 4)
print(result)
print(add(10, 20) * 2)

When Python reaches return, it leaves the function and the call is replaced by the returned value. So add(3, 4) becomes 7.

This is the most important idea in this lesson.

  • print() shows a value to the person looking at the screen. The program itself cannot use it afterwards.
  • return gives a value back to the program, so the rest of your code can use it.

Compare these two functions.

Try it yourselfPython
def add_and_print(a, b):
    print(a + b)

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

x = add_and_print(3, 4)
y = add_and_return(3, 4)

print("x is", x)
print("y is", y)

add_and_print shows 7 on the screen, but x is None: nothing came back. add_and_return shows nothing by itself, but y holds 7 and can be used.

Using returned values

Because a call becomes a value, you can use it anywhere a value fits.

Try it yourselfPython
def to_fahrenheit(celsius):
    return celsius * 9 / 5 + 32

temperature = to_fahrenheit(25)
print(temperature)

if to_fahrenheit(40) > 100:
    print("That is hot!")

readings = [0, 20, 37]
for reading in readings:
    print(reading, "C is", to_fahrenheit(reading), "F")

return ends the function

As soon as a return runs, the function stops. Nothing after it in the function runs.

Try it yourselfPython
def check_age(age):
    if age < 0:
        return "Invalid age"
    if age < 18:
        return "Minor"
    return "Adult"

print(check_age(-5))
print(check_age(12))
print(check_age(30))

This lets you handle special cases first and leave early, which often removes the need for else.

Returning True or False

Functions that answer a yes-or-no question return a boolean. Their names usually start with is_ or has_.

Try it yourselfPython
def is_even(number):
    return number % 2 == 0

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

for number in range(1, 6):
    if is_even(number):
        print(number, "is even")

Returning more than one value

Separate the values with commas. Python packs them into a tuple, which you can unpack on the other side.

Try it yourselfPython
def min_and_max(numbers):
    return min(numbers), max(numbers)

lowest, highest = min_and_max([72, 95, 61, 88])

print("Lowest:", lowest)
print("Highest:", highest)

Common mistakes

Practice exercise

Write a function square that returns the square of its argument.

Your solutionPython
# Define a function called square that takes a number
# and RETURNS that number multiplied by itself.


# Do not change the line below. Output must be exactly: 49
print(square(7))
Knowledge check

What does a function return if it has no return statement?

Summary

  • return hands a value back to the code that called the function.
  • print shows a value. return gives it back so it can be used.
  • A function without return gives back None.
  • return ends the function immediately.
  • A function can return several values, separated by commas.

Next: where variables live, and why a function cannot see everything.

Finished reading? Tick it off to track your progress.