WEBVTT 00:00:00.981 --> 00:00:03.728 We're back with our program that uses a function 00:00:03.728 --> 00:00:05.575 to draw Winston four times. 00:00:05.575 --> 00:00:08.156 And I've changed it to actually show what age he is 00:00:08.156 --> 00:00:10.587 at each point of life, and you'll see why soon. 00:00:11.377 --> 00:00:13.549 But first I need to tell you something about Winston: 00:00:13.549 --> 00:00:15.901 Winston has an addiction to donuts. 00:00:15.901 --> 00:00:18.437 He eats three of them a day. 00:00:18.437 --> 00:00:21.333 It's probably why his face is so big and yellow. 00:00:21.703 --> 00:00:25.311 So to warn Winston about how bad donuts are for him, 00:00:25.311 --> 00:00:28.263 I wanna modify this program to show how many donuts 00:00:28.263 --> 00:00:31.755 he's eaten total at each point in his life. 00:00:32.095 --> 00:00:35.783 For example, when he's two years old, 00:00:35.873 --> 00:00:41.598 that means he's eaten 3 times 365 times 2, 00:00:41.598 --> 00:00:45.873 so three in a day, times 365 days, times 2 years. 00:00:45.873 --> 00:00:50.330 And we'll just display that underneath that header, so wow! 00:00:50.330 --> 00:00:53.945 Two thousand donuts, that is a lot of donuts for a two-year-old. 00:00:53.945 --> 00:01:02.452 Now when he's 14 years old, let's see, he's had 3 times 365 times 14 00:01:04.032 --> 00:01:08.446 And that is 15,000 donuts. All right. 00:01:08.446 --> 00:01:12.620 So I could keep doing this math, but I'm beginning to notice a pattern. 00:01:13.150 --> 00:01:16.718 I'm repeating my calculation here, 00:01:16.718 --> 00:01:20.416 and I'm just changing one thing about it: the number of years. 00:01:20.416 --> 00:01:22.872 Whenever I see repeated code like this, 00:01:22.872 --> 00:01:28.348 I think to myself, "Hmm, can I make that into a function?" 00:01:28.348 --> 00:01:31.835 Yeah, definitely, we can. So let's do that now. 00:01:32.795 --> 00:01:34.416 I'll define my function up here 00:01:34.416 --> 00:01:40.377 and call it calcTotalDonuts = function 00:01:40.917 --> 00:01:44.228 And it's going to take one parameter, the number of years, 00:01:44.228 --> 00:01:46.022 because that's the only thing that we're changing 00:01:46.022 --> 00:01:48.566 each time we do this calculation. 00:01:49.316 --> 00:01:52.878 And then inside, we'll do the calculation, 00:01:52.878 --> 00:01:59.600 and save it into a variable, so it'll be 3 times 365 times numYears. 00:02:00.680 --> 00:02:03.099 All right, so now that we have that function, 00:02:03.099 --> 00:02:08.598 I'm going to replace this expression here with calcTotalDonuts, 00:02:08.598 --> 00:02:11.568 the call to the function, and passing in "2". 00:02:12.738 --> 00:02:16.877 Okay, um, well now we don't see any total at all. 00:02:17.227 --> 00:02:19.092 Hmm, okay, what happened? 00:02:19.092 --> 00:02:22.616 Well, our function did calculate the total, here, 00:02:23.096 --> 00:02:25.640 but it didn't tell anybody about that total. 00:02:26.050 --> 00:02:30.367 It's like if a teacher calls on you in class to answer a question 00:02:30.367 --> 00:02:31.744 and you answer it in your head 00:02:31.744 --> 00:02:33.652 but you're too shy to say the answer. 00:02:33.652 --> 00:02:36.998 You've done the work, but your teacher's never gonna know about it. 00:02:37.348 --> 00:02:39.679 If we want the function to communicate a value 00:02:39.679 --> 00:02:45.170 to whoever called it, the function has to explicitly return the value. 00:02:45.710 --> 00:02:49.319 So for a function to return a value, we need to type return, 00:02:49.599 --> 00:02:51.568 and then whatever it wants to return. 00:02:51.568 --> 00:02:55.138 Maybe it's a variable or a value or an expression, 00:02:55.138 --> 00:02:57.749 so here we'll say return totalDonuts, okay? 00:02:57.749 --> 00:03:01.540 So whoever's calling that function's going to get this response. 00:03:01.880 --> 00:03:04.421 And now our value displays, yay! 00:03:04.771 --> 00:03:06.651 And actually we can shorten our function, 00:03:06.651 --> 00:03:08.341 we don't even have to store it into a variable, 00:03:08.341 --> 00:03:10.051 we can just take this whole expression here 00:03:10.051 --> 00:03:14.268 put it in the return, and then it's just a one-liner. Nice. 00:03:15.068 --> 00:03:17.166 All right, so now we can go through 00:03:17.166 --> 00:03:20.444 and calculate the total donuts at each point in life 00:03:20.444 --> 00:03:23.452 by just calling this function and passing in the number of years. 00:03:24.212 --> 00:03:28.704 Um, let's see, it's calcTotalDonuts(25), 00:03:28.704 --> 00:03:34.056 position is correctly, calcTotalDonuts(65) 00:03:36.366 --> 00:03:40.241 Okay! Wow, so if he makes it to 65, 00:03:40.241 --> 00:03:44.406 he will eat 70,000 donuts. That is a lot of donuts. 00:03:45.136 --> 00:03:48.051 I don't think Winston's gonna make it. (laugh) 00:03:48.051 --> 00:03:50.331 But now that we've made it into a function, 00:03:50.331 --> 00:03:52.891 it's really easy for us to change parts of it. 00:03:53.181 --> 00:03:55.138 Like if Winston sees this and says, 00:03:55.138 --> 00:03:58.865 "Whoa, whoa, that's a lot. What if I just ate one a day?" 00:03:58.865 --> 00:04:02.242 Okay, well we can just go here, change this one number, 00:04:02.732 --> 00:04:04.329 and see everything change. 00:04:04.329 --> 00:04:07.971 So that's 23,000. Still a lot. So maybe Winston's like, 00:04:07.971 --> 00:04:10.773 "All right, all right, what if I just had one a week?" 00:04:10.773 --> 00:04:12.477 Yeah, that's reasonable, okay. 00:04:12.477 --> 00:04:15.025 So then we could just change 365 to 50, 00:04:15.025 --> 00:04:17.153 because there's like 50 weeks in a year, right? 00:04:18.103 --> 00:04:22.802 Okay, that's a lot better, right? 3,000 donuts, that seems reasonable. 00:04:22.802 --> 00:04:26.751 All right, so if you see, with functions and return values, 00:04:26.751 --> 00:04:30.631 we can save code and save lives.