What does return actually do? (specific exercise)

Hello all,

First off, I know that this question has been asked before. However, the explanations are just not clicking for me…

I’m confused as to what the ‘return’ function actually does. In this exercise you’ll be calculating your age. I removed ‘return age’ on line 3 to see what it does, and the output is:

I am None years old and my dad is None years old

Why will it not calculate the age if you don’t add the return function in your code? I added the code without the return function below.

Sorry I’m asking this question again, I know it’s a bit dumb but I’m just not getting it :frowning:

Thank you for your help!

def calculate_age(current_year, birth_year):

  age = current_year - birth_year

  

my_age = calculate_age(2049, 1993)

dads_age = calculate_age(2049, 1953)

print("I am " + str(my_age) + " years old" + " and my dad is " + str(dads_age) + " years old")

https://www.codecademy.com/forum_questions/51c0e35d7c82caace80008b1
Here is a link to a read only forum regarding this. I hope it helps!

(I don’t really know much Python, but I generally try to help as much as possible)

Also, welcome to the forums!

1 Like

So the best way to think about this is thinking about the purpose of a function. It’s a piece of code where you give it some inputs, and then it outputs what you want it to do, that way instead of calculating say, 500 ages manually, you just put down the calculation, and you can substitute in whatever you want and it’ll do the same thing to it.

Now for a function like this, you need an input so that it knows what you want used for this specific instance of the calculation, in this case you need the current year, and the persons birth year, otherwise it doesn’t know what age you want! However you also need an output from the function, otherwise you’ll have no idea what was actually calculated. You use a print statement inside the function call to get an output, for example:

def calculate_age(current_year, birth_year):
   age = current_year - birth_year

   print("Current age is " + age)

calculate_age(2049, 1953)
# prints "Current age is 96"

However this has limitations. What if you want to calculate the difference between your age and your dads age? Well then you’ll need to perform the age calculations manually right now, as there’s no easy way to get the age from the function.

This is where return comes in. Instead of printing a value to the terminal when the program is run, you can return a value from the function. It’s like if you gave a shopkeeper some money, that is your input, and the return would be the product that the shopkeeper gives you in exchange for that money. This allows you to save the age value to a variable, as effectively the value of the function is whatever is returned. So when you do:

def calculate_age(current_year, birth_year):

  age = current_year - birth_year
  return age

my_age = calculate_age(2049, 1993)

The function performs the calculation using 2049 and 1993, and takes the value of the calculation, in this case, 56. Then the variable my_age also takes the value of 56, as that is what is being defined above. When the return statement is taken out, the function is still actually performing the calculation, it just has no way to tell you what that value is. Using our example before, you can now find out the difference between your age and your dads age like so:

my_age = calculate_age(2049, 1993)
# my_age takes the value of 56

dads_age = calculate_age(2049, 1953)
# dads_age takes the value of 96

diff = dads_age - my_age
# diff takes the value of 40

TL:DR; The return in a function allows the function to tell you the value it has calculated, and allows you to use the value later in the code.

3 Likes

That’s helpful, thank you so much!

1 Like

Thank you so much!! It makes sense now :slight_smile: Thanks a lot!

1 Like