We're back with our program that uses a function
to draw Winston four times.
And I've changed it to actually show what age he is
at each point of life, and you'll see why soon.
But first I need to tell you something about Winston:
Winston has an addiction to donuts.
He eats three of them a day.
It's probably why his face is so big and yellow.
So to warn Winston about how bad donuts are for him,
I wanna modify this program to show how many donuts
he's eaten total at each point in his life.
For example, when he's two years old,
that means he's eaten 3 times 365 times 2,
so three in a day, times 365 days, times 2 years.
And we'll just display that underneath that header, so wow!
Two thousand donuts, that is a lot of donuts for a two-year-old.
Now when he's 14 years old, let's see, he's had 3 times 365 times 14
And that is 15,000 donuts. All right.
So I could keep doing this math, but I'm beginning to notice a pattern.
I'm repeating my calculation here,
and I'm just changing one thing about it: the number of years.
Whenever I see repeated code like this,
I think to myself, "Hmm, can I make that into a function?"
Yeah, definitely, we can. So let's do that now.
I'll define my function up here
and call it calcTotalDonuts = function
And it's going to take one parameter, the number of years,
because that's the only thing that we're changing
each time we do this calculation.
And then inside, we'll do the calculation,
and save it into a variable, so it'll be 3 times 365 times numYears.
All right, so now that we have that function,
I'm going to replace this expression here with calcTotalDonuts,
the call to the function, and passing in "2".
Okay, um, well now we don't see any total at all.
Hmm, okay, what happened?
Well, our function did calculate the total, here,
but it didn't tell anybody about that total.
It's like if a teacher calls on you in class to answer a question
and you answer it in your head
but you're too shy to say the answer.
You've done the work, but your teacher's never gonna know about it.
If we want the function to communicate a value
to whoever called it, the function has to explicitly return the value.
So for a function to return a value, we need to type return,
and then whatever it wants to return.
Maybe it's a variable or a value or an expression,
so here we'll say return totalDonuts, okay?
So whoever's calling that function's going to get this response.
And now our value displays, yay!
And actually we can shorten our function,
we don't even have to store it into a variable,
we can just take this whole expression here
put it in the return, and then it's just a one-liner. Nice.
All right, so now we can go through
and calculate the total donuts at each point in life
by just calling this function and passing in the number of years.
Um, let's see, it's calcTotalDonuts(25),
position is correctly, calcTotalDonuts(65)
Okay! Wow, so if he makes it to 65,
he will eat 70,000 donuts. That is a lot of donuts.
I don't think Winston's gonna make it. (laugh)
But now that we've made it into a function,
it's really easy for us to change parts of it.
Like if Winston sees this and says,
"Whoa, whoa, that's a lot. What if I just ate one a day?"
Okay, well we can just go here, change this one number,
and see everything change.
So that's 23,000. Still a lot. So maybe Winston's like,
"All right, all right, what if I just had one a week?"
Yeah, that's reasonable, okay.
So then we could just change 365 to 50,
because there's like 50 weeks in a year, right?
Okay, that's a lot better, right? 3,000 donuts, that seems reasonable.
All right, so if you see, with functions and return values,
we can save code and save lives.