Return to Video

Function Return Values (Video Version)

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

more » « less
Video Language:
English
Duration:
04:32

English subtitles

Revisions