The return statement is a very simple one. It just finishes the running of code in the current function and "returns" to the function’s caller.
As you have seen, functions are used to make code more understandable, cleaner and more organized. Say we have a couple of functions in our program:
minor_hit = 3
major_hit = 7
def adjust_health(player, hit):
player[health] = player[health] - hit
if (is_alive(player) == false):
return player_dead
return player_alive
def is_alive(player, hit):
if (player[health] >= 20):
return true
else: # player has died!
return falseIf someplace in our code we were to do something like:
# big hit!
continue_playing = adjust_health(player_one, major_hit)
if (continue_playing == player_dead):
end_game()You can see how when we call the function "adjust_health()" it returns either player_alive or player_dead, and we make a decision to end the game if the player has died.
Notice too, you can have multiple return statements in functions, and each one can return a different value if that’s what you need.