Lesson 28 of 4070%
(Progress persistence is disabled until Phase 9)
Python
Beginner
Return Values
Output data from a function back to the caller.
Return Values
What You’ll Learn
You’ll learn how to let a function return a value back to the code that called it.
The return Keyword
To let a function return a value, use the return statement.
def multiply(x, y):
return x * y
result = multiply(5, 3)
print(result) # 15
When a return statement is reached, the function immediately stops executing and gives the value back.
def check_age(age):
if age < 18:
return "Too young"
# This won't run if age < 18
return "Welcome!"
print(check_age(15)) # Too young
print(check_age(20)) # Welcome!
The None Type
If a function doesn’t have a return statement (like our print() examples from earlier), it implicitly returns a special Python value called None.