Return Value

I just finished the Return Value tutorial but I actually still don’t understand how does the ‘return’ command works?

def calculate_age(current_year, birth_year):
  age = current_year - birth_year
  return age
my_age = calculate_age (2049, 1993)
dads_age = calculate_age (2049, 1953)

x = my_age
y = dads_age

print("I am" + " " + str(x) + ' ' + "years old")
print("My dad is" + " " + str(y) + " " + "years old")
1 Like

return literally hands back data to the function call:

# return value is stored in my_age variable
my_age = calculate_age (2049, 1993)

this useful for several reasons, one because we can not access the age variable outside the function (it has local scope), and two, it allows us to pass data around even between functions

3 Likes