Functions can return
a value that you can use directly:
def give_me_five():
return 5
print(give_me_five()) # Print the returned value
# Out: 5
or save the value for later use:
num = give_me_five()
print(num) # Print the saved returned value
# Out: 5
or use the value for any operations:
print(give_me_five() + 10)
# Out: 15
If return
is encountered in the function the function will be exited immediately and subsequent operations will not be evaluated:
def give_me_another_five():
return 5
print('This statement will not be printed. Ever.')
print(give_me_another_five())
# Out: 5
You can also return
multiple values (in the form of a tuple):
def give_me_two_fives():
return 5, 5 # Returns two 5
first, second = give_me_two_fives()
print(first)
# Out: 5
print(second)
# Out: 5
A function with no return
statement implicitly returns None
. Similarly a function with a return
statement, but no return value or variable returns None
.