Function Return Values (Video Version)
-
0:01 - 0:04We're back with our program
that uses a function -
0:04 - 0:06to draw Winston four times.
-
0:06 - 0:08And I've changed it
to actually show what age he is -
0:08 - 0:11at each point of life,
and you'll see why soon. -
0:11 - 0:14But first I need to tell you
something about Winston: -
0:14 - 0:16Winston has an addiction to donuts.
-
0:16 - 0:18He eats three of them a day.
-
0:18 - 0:21It's probably why his face
is so big and yellow. -
0:22 - 0:25So to warn Winston about
how bad donuts are for him, -
0:25 - 0:28I wanna modify this program
to show how many donuts -
0:28 - 0:32he's eaten total
at each point in his life. -
0:32 - 0:36For example, when he's two years old,
-
0:36 - 0:42that means he's
eaten 3 times 365 times 2, -
0:42 - 0:46so three in a day,
times 365 days, times 2 years. -
0:46 - 0:50And we'll just display that
underneath that header, so wow! -
0:50 - 0:54Two thousand donuts, that
is a lot of donuts for a two-year-old. -
0:54 - 1:02Now when he's 14 years old, let's see,
he's had 3 times 365 times 14 -
1:04 - 1:08And that is 15,000 donuts. All right.
-
1:08 - 1:13So I could keep doing this math,
but I'm beginning to notice a pattern. -
1:13 - 1:17I'm repeating my calculation here,
-
1:17 - 1:20and I'm just changing one thing about it:
the number of years. -
1:20 - 1:23Whenever I see repeated code like this,
-
1:23 - 1:28I think to myself, "Hmm,
can I make that into a function?" -
1:28 - 1:32Yeah, definitely, we can.
So let's do that now. -
1:33 - 1:34I'll define my function up here
-
1:34 - 1:40and call it calcTotalDonuts = function
-
1:41 - 1:44And it's going to take one parameter,
the number of years, -
1:44 - 1:46because that's the only thing
that we're changing -
1:46 - 1:49each time we do this calculation.
-
1:49 - 1:53And then inside, we'll do the calculation,
-
1:53 - 2:00and save it into a variable,
so it'll be 3 times 365 times numYears. -
2:01 - 2:03All right, so now that we
have that function, -
2:03 - 2:09I'm going to replace this expression here
with calcTotalDonuts, -
2:09 - 2:12the call to the function,
and passing in "2". -
2:13 - 2:17Okay, um, well now we
don't see any total at all. -
2:17 - 2:19Hmm, okay, what happened?
-
2:19 - 2:23Well, our function
did calculate the total, here, -
2:23 - 2:26but it didn't tell anybody
about that total. -
2:26 - 2:30It's like if a teacher calls on you
in class to answer a question -
2:30 - 2:32and you answer it in your head
-
2:32 - 2:34but you're too shy to say the answer.
-
2:34 - 2:37You've done the work, but your teacher's
never gonna know about it. -
2:37 - 2:40If we want the function
to communicate a value -
2:40 - 2:45to whoever called it, the function
has to explicitly return the value. -
2:46 - 2:49So for a function to return a value,
we need to type return, -
2:50 - 2:52and then whatever it wants to return.
-
2:52 - 2:55Maybe it's a variable
or a value or an expression, -
2:55 - 2:58so here we'll say
return totalDonuts, okay? -
2:58 - 3:02So whoever's calling that function's
going to get this response. -
3:02 - 3:04And now our value displays, yay!
-
3:05 - 3:07And actually we
can shorten our function, -
3:07 - 3:08we don't even have to
store it into a variable, -
3:08 - 3:10we can just take
this whole expression here -
3:10 - 3:14put it in the return,
and then it's just a one-liner. Nice. -
3:15 - 3:17All right, so now we can go through
-
3:17 - 3:20and calculate the total donuts
at each point in life -
3:20 - 3:23by just calling this function
and passing in the number of years. -
3:24 - 3:29Um, let's see, it's calcTotalDonuts(25),
-
3:29 - 3:34position is correctly, calcTotalDonuts(65)
-
3:36 - 3:40Okay! Wow, so if he makes it to 65,
-
3:40 - 3:44he will eat 70,000 donuts.
That is a lot of donuts. -
3:45 - 3:48I don't think Winston's
gonna make it. (laugh) -
3:48 - 3:50But now that we've
made it into a function, -
3:50 - 3:53it's really easy for us
to change parts of it. -
3:53 - 3:55Like if Winston sees this and says,
-
3:55 - 3:59"Whoa, whoa, that's a lot.
What if I just ate one a day?" -
3:59 - 4:02Okay, well we can just go here,
change this one number, -
4:03 - 4:04and see everything change.
-
4:04 - 4:08So that's 23,000. Still a lot.
So maybe Winston's like, -
4:08 - 4:11"All right, all right, what if
I just had one a week?" -
4:11 - 4:12Yeah, that's reasonable, okay.
-
4:12 - 4:15So then we could just change 365 to 50,
-
4:15 - 4:17because there's like
50 weeks in a year, right? -
4:18 - 4:23Okay, that's a lot better, right?
3,000 donuts, that seems reasonable. -
4:23 - 4:27All right, so if you see,
with functions and return values, -
4:27 - 4:31we can save code and save lives.
- Title:
- Function Return Values (Video Version)
- Description:
-
This is just a screen grab of our interactive coding talk-through, prepared to make captioning and translation easier. It is better to watch our talk-throughs here:
https://www.khanacademy.org/cs/programming/ - Video Language:
- English
- Duration:
- 04:32
Jennifer Fennerl edited English subtitles for Function Return Values (Video Version) | ||
Jennifer Fennerl edited English subtitles for Function Return Values (Video Version) | ||
Jennifer Fennerl edited English subtitles for Function Return Values (Video Version) | ||
kramtark edited English subtitles for Function Return Values (Video Version) |