[Script Info] Title: [Events] Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text Dialogue: 0,0:00:00.98,0:00:03.73,Default,,0000,0000,0000,,We're back with our program\Nthat uses a function Dialogue: 0,0:00:03.73,0:00:05.58,Default,,0000,0000,0000,,to draw Winston four times. Dialogue: 0,0:00:05.58,0:00:08.16,Default,,0000,0000,0000,,And I've changed it\Nto actually show what age he is Dialogue: 0,0:00:08.16,0:00:10.59,Default,,0000,0000,0000,,at each point of life,\Nand you'll see why soon. Dialogue: 0,0:00:11.38,0:00:13.55,Default,,0000,0000,0000,,But first I need to tell you\Nsomething about Winston: Dialogue: 0,0:00:13.55,0:00:15.90,Default,,0000,0000,0000,,Winston has an addiction to donuts. Dialogue: 0,0:00:15.90,0:00:18.44,Default,,0000,0000,0000,,He eats three of them a day. Dialogue: 0,0:00:18.44,0:00:21.33,Default,,0000,0000,0000,,It's probably why his face\Nis so big and yellow. Dialogue: 0,0:00:21.70,0:00:25.31,Default,,0000,0000,0000,,So to warn Winston about\Nhow bad donuts are for him, Dialogue: 0,0:00:25.31,0:00:28.26,Default,,0000,0000,0000,,I wanna modify this program\Nto show how many donuts Dialogue: 0,0:00:28.26,0:00:31.76,Default,,0000,0000,0000,,he's eaten total\Nat each point in his life. Dialogue: 0,0:00:32.10,0:00:35.78,Default,,0000,0000,0000,,For example, when he's two years old, Dialogue: 0,0:00:35.87,0:00:41.60,Default,,0000,0000,0000,,that means he's\Neaten 3 times 365 times 2, Dialogue: 0,0:00:41.60,0:00:45.87,Default,,0000,0000,0000,,so three in a day,\Ntimes 365 days, times 2 years. Dialogue: 0,0:00:45.87,0:00:50.33,Default,,0000,0000,0000,,And we'll just display that\Nunderneath that header, so wow! Dialogue: 0,0:00:50.33,0:00:53.94,Default,,0000,0000,0000,,Two thousand donuts, that\Nis a lot of donuts for a two-year-old. Dialogue: 0,0:00:53.94,0:01:02.45,Default,,0000,0000,0000,,Now when he's 14 years old, let's see,\Nhe's had 3 times 365 times 14 Dialogue: 0,0:01:04.03,0:01:08.45,Default,,0000,0000,0000,,And that is 15,000 donuts. All right. Dialogue: 0,0:01:08.45,0:01:12.62,Default,,0000,0000,0000,,So I could keep doing this math,\Nbut I'm beginning to notice a pattern. Dialogue: 0,0:01:13.15,0:01:16.72,Default,,0000,0000,0000,,I'm repeating my calculation here, Dialogue: 0,0:01:16.72,0:01:20.42,Default,,0000,0000,0000,,and I'm just changing one thing about it:\Nthe number of years. Dialogue: 0,0:01:20.42,0:01:22.87,Default,,0000,0000,0000,,Whenever I see repeated code like this, Dialogue: 0,0:01:22.87,0:01:28.35,Default,,0000,0000,0000,,I think to myself, "Hmm,\Ncan I make that into a function?" Dialogue: 0,0:01:28.35,0:01:31.84,Default,,0000,0000,0000,,Yeah, definitely, we can.\NSo let's do that now. Dialogue: 0,0:01:32.80,0:01:34.42,Default,,0000,0000,0000,,I'll define my function up here Dialogue: 0,0:01:34.42,0:01:40.38,Default,,0000,0000,0000,,and call it calcTotalDonuts = function Dialogue: 0,0:01:40.92,0:01:44.23,Default,,0000,0000,0000,,And it's going to take one parameter,\Nthe number of years, Dialogue: 0,0:01:44.23,0:01:46.02,Default,,0000,0000,0000,,because that's the only thing\Nthat we're changing Dialogue: 0,0:01:46.02,0:01:48.57,Default,,0000,0000,0000,,each time we do this calculation. Dialogue: 0,0:01:49.32,0:01:52.88,Default,,0000,0000,0000,,And then inside, we'll do the calculation, Dialogue: 0,0:01:52.88,0:01:59.60,Default,,0000,0000,0000,,and save it into a variable,\Nso it'll be 3 times 365 times numYears. Dialogue: 0,0:02:00.68,0:02:03.10,Default,,0000,0000,0000,,All right, so now that we\Nhave that function, Dialogue: 0,0:02:03.10,0:02:08.60,Default,,0000,0000,0000,,I'm going to replace this expression here\Nwith calcTotalDonuts, Dialogue: 0,0:02:08.60,0:02:11.57,Default,,0000,0000,0000,,the call to the function,\Nand passing in "2". Dialogue: 0,0:02:12.74,0:02:16.88,Default,,0000,0000,0000,,Okay, um, well now we\Ndon't see any total at all. Dialogue: 0,0:02:17.23,0:02:19.09,Default,,0000,0000,0000,,Hmm, okay, what happened? Dialogue: 0,0:02:19.09,0:02:22.62,Default,,0000,0000,0000,,Well, our function\Ndid calculate the total, here, Dialogue: 0,0:02:23.10,0:02:25.64,Default,,0000,0000,0000,,but it didn't tell anybody\Nabout that total. Dialogue: 0,0:02:26.05,0:02:30.37,Default,,0000,0000,0000,,It's like if a teacher calls on you\Nin class to answer a question Dialogue: 0,0:02:30.37,0:02:31.74,Default,,0000,0000,0000,,and you answer it in your head Dialogue: 0,0:02:31.74,0:02:33.65,Default,,0000,0000,0000,,but you're too shy to say the answer. Dialogue: 0,0:02:33.65,0:02:36.100,Default,,0000,0000,0000,,You've done the work, but your teacher's\Nnever gonna know about it. Dialogue: 0,0:02:37.35,0:02:39.68,Default,,0000,0000,0000,,If we want the function\Nto communicate a value Dialogue: 0,0:02:39.68,0:02:45.17,Default,,0000,0000,0000,,to whoever called it, the function\Nhas to explicitly return the value. Dialogue: 0,0:02:45.71,0:02:49.32,Default,,0000,0000,0000,,So for a function to return a value,\Nwe need to type return, Dialogue: 0,0:02:49.60,0:02:51.57,Default,,0000,0000,0000,,and then whatever it wants to return. Dialogue: 0,0:02:51.57,0:02:55.14,Default,,0000,0000,0000,,Maybe it's a variable\Nor a value or an expression, Dialogue: 0,0:02:55.14,0:02:57.75,Default,,0000,0000,0000,,so here we'll say\Nreturn totalDonuts, okay? Dialogue: 0,0:02:57.75,0:03:01.54,Default,,0000,0000,0000,,So whoever's calling that function's\Ngoing to get this response. Dialogue: 0,0:03:01.88,0:03:04.42,Default,,0000,0000,0000,,And now our value displays, yay! Dialogue: 0,0:03:04.77,0:03:06.65,Default,,0000,0000,0000,,And actually we\Ncan shorten our function, Dialogue: 0,0:03:06.65,0:03:08.34,Default,,0000,0000,0000,,we don't even have to\Nstore it into a variable, Dialogue: 0,0:03:08.34,0:03:10.05,Default,,0000,0000,0000,,we can just take\Nthis whole expression here Dialogue: 0,0:03:10.05,0:03:14.27,Default,,0000,0000,0000,,put it in the return,\Nand then it's just a one-liner. Nice. Dialogue: 0,0:03:15.07,0:03:17.17,Default,,0000,0000,0000,,All right, so now we can go through Dialogue: 0,0:03:17.17,0:03:20.44,Default,,0000,0000,0000,,and calculate the total donuts\Nat each point in life Dialogue: 0,0:03:20.44,0:03:23.45,Default,,0000,0000,0000,,by just calling this function\Nand passing in the number of years. Dialogue: 0,0:03:24.21,0:03:28.70,Default,,0000,0000,0000,,Um, let's see, it's calcTotalDonuts(25), Dialogue: 0,0:03:28.70,0:03:34.06,Default,,0000,0000,0000,,position is correctly, calcTotalDonuts(65) Dialogue: 0,0:03:36.37,0:03:40.24,Default,,0000,0000,0000,,Okay! Wow, so if he makes it to 65, Dialogue: 0,0:03:40.24,0:03:44.41,Default,,0000,0000,0000,,he will eat 70,000 donuts.\NThat is a lot of donuts. Dialogue: 0,0:03:45.14,0:03:48.05,Default,,0000,0000,0000,,I don't think Winston's\Ngonna make it. (laugh) Dialogue: 0,0:03:48.05,0:03:50.33,Default,,0000,0000,0000,,But now that we've\Nmade it into a function, Dialogue: 0,0:03:50.33,0:03:52.89,Default,,0000,0000,0000,,it's really easy for us\Nto change parts of it. Dialogue: 0,0:03:53.18,0:03:55.14,Default,,0000,0000,0000,,Like if Winston sees this and says, Dialogue: 0,0:03:55.14,0:03:58.86,Default,,0000,0000,0000,,"Whoa, whoa, that's a lot.\NWhat if I just ate one a day?" Dialogue: 0,0:03:58.86,0:04:02.24,Default,,0000,0000,0000,,Okay, well we can just go here,\Nchange this one number, Dialogue: 0,0:04:02.73,0:04:04.33,Default,,0000,0000,0000,,and see everything change. Dialogue: 0,0:04:04.33,0:04:07.97,Default,,0000,0000,0000,,So that's 23,000. Still a lot.\NSo maybe Winston's like, Dialogue: 0,0:04:07.97,0:04:10.77,Default,,0000,0000,0000,,"All right, all right, what if\NI just had one a week?" Dialogue: 0,0:04:10.77,0:04:12.48,Default,,0000,0000,0000,,Yeah, that's reasonable, okay. Dialogue: 0,0:04:12.48,0:04:15.02,Default,,0000,0000,0000,,So then we could just change 365 to 50, Dialogue: 0,0:04:15.02,0:04:17.15,Default,,0000,0000,0000,,because there's like\N50 weeks in a year, right? Dialogue: 0,0:04:18.10,0:04:22.80,Default,,0000,0000,0000,,Okay, that's a lot better, right?\N3,000 donuts, that seems reasonable. Dialogue: 0,0:04:22.80,0:04:26.75,Default,,0000,0000,0000,,All right, so if you see,\Nwith functions and return values, Dialogue: 0,0:04:26.75,0:04:30.63,Default,,0000,0000,0000,,we can save code {\i1}and{\i0} save lives.