← All lessons

return: get the result back

Sign in · Sign up

Lesson 6.2

return hands a value back from a function — with it you can save the result and use it afterwards:

def add(a, b):
    ...                     # return the sum

The difference from print is short: print shows, return returns. return ends the function right away — the code after it does not run.

Write a function add(a, b) that returns the sum of two numbers.

Hint

return returns a value and ends the function immediately.

print only shows something and returns nothing: without return the function gives None. The sum is returned, and the call prints it.