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