Return to Video

Intro To Variables (Video Version)

  • 0:01 - 0:04
    So I've got my good friend Winston here
    to help us out with this one.
  • 0:04 - 0:06
    And I know he already looks
    impossibly awesome,
  • 0:06 - 0:09
    but I think I want to change
    his eye size a little bit.
  • 0:09 - 0:11
    So here is where I draw his eyes.
  • 0:11 - 0:13
    You can see we set the fill color,
    and then drop two ellipses.
  • 0:13 - 0:15
    And if I want to make
    this ellipse smaller,
  • 0:15 - 0:18
    I can change the width,
    but then I also want it to be round,
  • 0:18 - 0:20
    so I'm going to change the height
    to be the same thing.
  • 0:20 - 0:23
    And, well I want the eyes
    to be the same size,
  • 0:23 - 0:25
    so we should change the width
    and the height of this eye.
  • 0:25 - 0:28
    No, now the eyes are a little too small,
    so I should change them back.
  • 0:29 - 0:31
    Wouldn't I be great if I could
    just change one number,
  • 0:31 - 0:33
    and have both eyes change?
  • 0:33 - 0:36
    And here is where variables come in.
  • 0:37 - 0:41
    Now, a variable is just like a name
    or a placeholder for something else.
  • 0:41 - 0:45
    I like to think of a variable
    as a big bucket with a name on it.
  • 0:45 - 0:46
    You can put anything
    you want in the bucket,
  • 0:46 - 0:48
    but the name stays the same.
  • 0:48 - 0:52
    We call the thing inside the bucket
    the value of the variable.
  • 0:54 - 0:56
    So let's start out by actually
    making a variable.
  • 0:56 - 0:58
    I'm going to do it all the way up here.
  • 0:58 - 1:00
    All you do is type var,
    which stands for variable,
  • 1:00 - 1:02
    Space, and then
    the name of the variable,
  • 1:02 - 1:04
    which should describe
    what the variable will hold.
  • 1:04 - 1:06
    So I'm going to call this one eyeSize.
  • 1:06 - 1:08
    And you can pretty much
    name it whatever you want,
  • 1:08 - 1:10
    but you're not allowed to have
    spaces between words.
  • 1:10 - 1:12
    And of course,
    don't forget that semicolon!
  • 1:12 - 1:14
    So now I've made
    a variable called eyeSize,
  • 1:14 - 1:17
    because it's going to hold
    the size of Winston's eyes.
  • 1:17 - 1:20
    But so far this variable
    doesn't have a value yet.
  • 1:20 - 1:23
    To give it one, we just say eyeSize,
  • 1:23 - 1:27
    and then an =, and then 20; .
  • 1:28 - 1:29
    Okay, now see that equal sign?
  • 1:29 - 1:32
    Forget whatever your math teacher
    taught you about it.
  • 1:32 - 1:35
    That equal sign does not mean "equals,"
    it means assignment.
  • 1:35 - 1:41
    This means we're assigning
    the value 20 to the variable eyeSize.
  • 1:42 - 1:44
    In other words,
    we're putting the number 20
  • 1:44 - 1:46
    into the bucket called, eyeSize.
  • 1:46 - 1:48
    And remember how in math class
    you can say stuff like,
  • 1:48 - 1:52
    x = 3, and 3 = x,
  • 1:52 - 1:54
    and it all means the same thing,
    because duuh, they're equal?
  • 1:54 - 1:56
    Well, you can't do that here, either.
  • 1:56 - 1:58
    The thing on the left-hand side
    of the equal sign
  • 1:58 - 2:00
    is always the variable.
  • 2:00 - 2:03
    And the thing on the right-hand side
    of the equal sign is always the value
  • 2:03 - 2:05
    that you are assigning to the variable.
  • 2:06 - 2:08
    A good way to help you
    remember which side is what
  • 2:08 - 2:10
    is while you're coding
    and talking out loud to yourself,
  • 2:10 - 2:11
    like every cool programmer does,
  • 2:11 - 2:15
    if you hit an equal sign
    don't say "equals," say "gets."
  • 2:15 - 2:19
    So this becomes, eyeSize gets 20.
  • 2:20 - 2:22
    And now, whenever I use
    eyeSize in my program,
  • 2:22 - 2:25
    the computer is going to be like,
    "Oh yeah, that's a variable,
  • 2:25 - 2:28
    I know what she really means
    is this value 20."
  • 2:28 - 2:31
    So check it out--
    I'm just going to copy this,
  • 2:31 - 2:34
    and then replace these four numbers
  • 2:35 - 2:37
    with my new variable eyeSize,
  • 2:38 - 2:40
    and Voila!
  • 2:40 - 2:44
    Winston's eyes are now both
    perfectly round and the same size,
  • 2:44 - 2:47
    and if I want to change
    the value of both eyes--
  • 2:47 - 2:48
    or the size of both eyes,
  • 2:48 - 2:51
    I can just change the value
    of this one variable.
  • 2:51 - 2:53
    Aah that's so cool!
  • 2:54 - 2:56
    Okay, couple of last notes.
  • 2:56 - 2:59
    Up here we made
    a new variable called eyeSize.
  • 2:59 - 3:01
    And here we gave it a value of 33.
  • 3:01 - 3:03
    We can actually do that all in one step
  • 3:03 - 3:06
    by saying var eyeSize,
    that's the first step,
  • 3:06 - 3:09
    gets 33,
    that's the second step.
  • 3:09 - 3:11
    And if I delete these two lines,
  • 3:11 - 3:13
    you can see everything still works.
  • 3:13 - 3:17
    Also remember that the computer
    reads your code from top to bottom
  • 3:17 - 3:19
    so the only reason it knew
    what eyeSize was down here,
  • 3:19 - 3:22
    was because we already
    defined it up here.
  • 3:22 - 3:24
    If I had put it down here instead,
  • 3:25 - 3:28
    then once we got to this line of code,
    the computer's going to be like,
  • 3:28 - 3:31
    "eyeSize, what the heck is eyeSize?
    I don't know what that is."
  • 3:31 - 3:34
    In fact, here we get an error that says
    eyeSize is not defined.
  • 3:34 - 3:36
    And maybe you're thinking to yourself,
  • 3:36 - 3:37
    "Yes, I did define it; it's right here!"
  • 3:37 - 3:40
    But the computer's not smart,
    and doesn't get that.
  • 3:40 - 3:43
    So let's just move it back up to the top.
  • 3:44 - 3:46
    And you've got to always make sure
  • 3:46 - 3:48
    that you define your variable
    before you try to use it.
  • 3:49 - 3:52
    And now you know
    about variables! Yaaay!
Title:
Intro To 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:
03:53

English subtitles

Revisions