Functions · Lesson 22 of 27
Return values and defaults
print shows a value. return gives a value back to whoever called the function, so you can use it.
def add(a, b):
return a + b
total = add(2, 3)
print(total * 10) # 50When Python reaches return, the function ends right there.
Default values
A parameter can have a default, used when you don't pass that argument:
def area(length, width=1):
return length * width
print(area(5, 3)) # 15
print(area(5)) # 5 (width is 1)In tests, functions are checked by what they return. If you print instead of return, the caller gets None.
Your task
Write a function power(base, exp=2) that returns base raised to exp.
power(3)→9power(2, 5)→32