Return to Video

Local and Global Variables (Video Version)

  • 0:01 - 0:03
    Now that you've mastered
    the basics of functions,
  • 0:03 - 0:07
    I want to talk about a topic
    that can get a little tricky:
  • 0:07 - 0:11
    the difference between
    local and global variables.
  • 0:11 - 0:14
    These terms may not
    mean anything to you now.
  • 0:14 - 0:16
    So let's start with an example.
  • 0:16 - 0:18
    I made this program here to show you
  • 0:18 - 0:20
    how many inches I grew in my childhood.
  • 0:21 - 0:22
    Since humans grow at different rates,
  • 0:22 - 0:24
    I came up with this function, calcInches,
  • 0:24 - 0:27
    where I can pass in
    a startAge and an endAge,
  • 0:27 - 0:29
    and an inchesPerYear,
  • 0:30 - 0:32
    and then it would calculate
    how many inches total
  • 0:32 - 0:34
    were grown during that time.
  • 0:35 - 0:37
    . . . and return it back
    to whoever called it.
  • 0:37 - 0:40
    So you can see here from 0 to 8 years
  • 0:40 - 0:44
    I call calcInches
    and I pass 0, 8, and 2.5,
  • 0:44 - 0:48
    because I grew about 2.5 inches per year.
  • 0:48 - 0:52
    And so it does a calculation,
    and you can see it spits out 20.
  • 0:52 - 0:58
    Then from 8 to 16, I call it from,
    and I pass it 8 and 16 and then 2
  • 0:58 - 1:01
    because I didn't quite grow as much,
    and you can see it spits out 16.
  • 1:02 - 1:05
    So this is neat, but now I
    want to actually display
  • 1:05 - 1:08
    how many inches I grew, total,
    in my whole childhood.
  • 1:09 - 1:14
    So how do I do that? Well, I
    might start by looking at my code
  • 1:14 - 1:17
    and thinking, "Hmm, what values
    do I have here?"
  • 1:17 - 1:20
    Do I have anything that looks
    like it represents total inches?
  • 1:21 - 1:26
    Well, I do have this totalInches variable
    inside my calcInches function,
  • 1:26 - 1:31
    so I could just output that,
    see what it says; start there.
  • 1:31 - 1:36
    So let's say text(totalInches, 10, 200),
    and put it down at the bottom.
  • 1:36 - 1:38
    All right, let's see, what have we got?
  • 1:38 - 1:41
    Oh, o-oh, we've got the 'Oh noes!' guy.
  • 1:41 - 1:43
    And he says there's a problem.
  • 1:43 - 1:46
    totalInches is not defined.
  • 1:46 - 1:48
    Well, that's weird, because we
  • 1:48 - 1:52
    defined totalInches right here, right?
    var totalInches =.
  • 1:52 - 1:54
    Well, the problem is that we declared
  • 1:54 - 1:59
    totalInches inside a function.
    On this line here.
  • 1:59 - 2:02
    And when we declare a variable
    inside a function,
  • 2:02 - 2:04
    it's considered a local variable.
  • 2:05 - 2:09
    It lives only inside
    this function here (calcInches).
  • 2:09 - 2:13
    And the code outside the function,
    all of this, it doesn't see
  • 2:13 - 2:16
    local variables inside functions.
  • 2:16 - 2:18
    It only sees whatever gets returned.
  • 2:18 - 2:21
    It only sees that value,
    not that variable.
  • 2:21 - 2:25
    So when we try to use totalInches
    outside the function,
  • 2:25 - 2:26
    it doesn't know what it is,
  • 2:26 - 2:29
    and it says 'Hey, I've never
    seen this variable before.
  • 2:29 - 2:31
    It's not defined. I can't display it.'
  • 2:32 - 2:35
    So there is a way that we can make it
  • 2:35 - 2:37
    so that the outside code
    can see this variable.
  • 2:37 - 2:42
    And that's if we turn it from
    a local variable into a global variable.
  • 2:43 - 2:47
    We can do that by moving the definition
    outside of the function,
  • 2:48 - 2:50
    into what's called the global scope.
  • 2:51 - 2:54
    And now, inside the function,
    all we're doing
  • 2:54 - 2:58
    is changing the value of it each time,
    not defining and declaring it.
  • 2:59 - 3:02
    So you can see that it says
    'Total grown over life: 16'
  • 3:03 - 3:06
    So it found the variable because
    we made it into a global variable.
  • 3:07 - 3:10
    But it's not actually the value
    that we're looking for.
  • 3:10 - 3:12
    That's just the most recent value.
  • 3:12 - 3:14
    And that's because every time
    we call this function,
  • 3:14 - 3:18
    it's setting totalInches to whatever it's
    calculating that time. Right?
  • 3:19 - 3:22
    So what we really want to do is,
    we want a new variable
  • 3:22 - 3:25
    that we use only
    for storing the overall total
  • 3:25 - 3:28
    that we add to every time we calculate,
    you know, the total for a range,
  • 3:29 - 3:33
    So let's change this back
    to being a local variable,
  • 3:34 - 3:38
    and let's make a new global variable
    called lifeInches,
  • 3:38 - 3:40
    and we'll start it at zero.
  • 3:41 - 3:45
    And then inside the function,
    we'll add to this global variable
  • 3:45 - 3:49
    by saying lifeInches += totalInches.
  • 3:49 - 3:52
    So we're going to add
    however much we calculate
  • 3:52 - 3:53
    each time we call this function,
  • 3:53 - 3:56
    we're going to add it
    to the lifeInches global variable.
  • 3:56 - 3:58
    And then at the bottom,
  • 3:58 - 4:00
    we'll display lifeInches:
    text(lifeInches, 10, 200).
  • 4:00 - 4:03
    Tada! our total growth over life.
  • 4:04 - 4:06
    Now that's not actually how tall I am.
    I'm taller than that.
  • 4:06 - 4:08
    But that's because you know,
  • 4:08 - 4:11
    we start off born with more than 0 length.
  • 4:11 - 4:14
    So if I want total,
    maybe I could start at 20.
  • 4:14 - 4:17
    And there you go, that's how tall I am.
  • 4:17 - 4:20
    All right. So let's review, totalInches
  • 4:20 - 4:22
    is what we call a local variable.
  • 4:22 - 4:24
    And we know that because
    we see the declaration of it
  • 4:24 - 4:27
    inside this function
    and not outside the function.
  • 4:29 - 4:31
    And so that means that
    this outside code here
  • 4:31 - 4:34
    doesn't know about this variable
    called totalInches.
  • 4:35 - 4:38
    Now lifeInches is what we
    call a global variable.
  • 4:38 - 4:40
    And we know that because
    we see its declaration
  • 4:40 - 4:43
    outside of any function
    in our global scope.
  • 4:44 - 4:45
    So try to keep this in mind when
  • 4:45 - 4:47
    you're writing your functions
    and your variables.
  • 4:47 - 4:49
    And think to yourself whether you want
  • 4:49 - 4:51
    a local variable
    for just your function to use,
  • 4:51 - 4:55
    or a global variable
    for your whole program to use.
  • 4:55 - 4:57
    And don't worry if this is hard
    to wrap your head around.
  • 4:57 - 5:00
    It's one of the trickiest
    concepts in programming,
  • 5:00 - 5:02
    and in JavaScript in particular.
  • 5:02 - 5:05
    And it's something you'll get better at
    as you keep practicing.
Title:
Local and Global Variables (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:
05:06

English subtitles

Revisions