Return to Video

Functions

  • 0:01 - 0:04
    Now let's talk about something
    you've been using
  • 0:04 - 0:06
    this whole time: Functions.
  • 0:06 - 0:09
    Whenever you've used commands
    like rect() or fill() or text(),
  • 0:09 - 0:12
    you've been calling functions,
    and those functions have drawn
  • 0:12 - 0:15
    what you've told them to do.
  • 0:15 - 0:17
    What is a function really?
  • 0:17 - 0:19
    It's a collection of code
    that we've grouped together
  • 0:19 - 0:21
    and given a name
    because we want to be able
  • 0:21 - 0:23
    to use that bit
    of functionality many times.
  • 0:23 - 0:26
    Think about rect()?
    What does a rect() do?
  • 0:26 - 0:29
    It just draws four lines, right?
  • 0:29 - 0:31
    We could just do that
    using our line() function, right?
  • 0:31 - 0:34
    And here we have
    what looks like a rectangle.
  • 0:34 - 0:37
    But we realize that we
    kind of want to be able
  • 0:37 - 0:39
    to draw a rectangle a lot of times,
  • 0:39 - 0:41
    and it would be really annoying
  • 0:41 - 0:43
    to do the math every time
    to try and figure out
  • 0:43 - 0:44
    how to draw a line
    from one corner to the next
  • 0:44 - 0:46
    and next and next.
  • 0:46 - 0:49
    So instead, we just made
    a rect() function,
  • 0:49 - 0:51
    and that function does exactly
    the same thing
  • 0:51 - 0:55
    that those four lines of code did,
    but in much less code.
  • 0:55 - 0:58
    So that's pretty cool,
    and rect() is one of those functions
  • 0:58 - 1:01
    that we've made available
    for ALL programs
  • 1:01 - 1:03
    to use here on Khan Academy.
  • 1:03 - 1:05
    But - you can also make up
    your own functions
  • 1:05 - 1:08
    to use in each of your programs.
  • 1:08 - 1:11
    For example - let's say
    we are making a program
  • 1:11 - 1:14
    and we want to draw Winston
    multiple times -
  • 1:14 - 1:17
    maybe cause we're going to tell
    the life story of Winston
  • 1:17 - 1:20
    and show him at every age
    in his life.
  • 1:20 - 1:24
    So, here's what our Winston
    drawing code might start off as:
  • 1:24 - 1:27
    We have 'faceX'
    and 'faceY' variables
  • 1:27 - 1:29
    to store the center of the face,
  • 1:29 - 1:31
    and then we draw the eyes
    and the mouth
  • 1:31 - 1:33
    relative to those variables.
  • 1:33 - 1:35
    Right now the program
    sees the code,
  • 1:35 - 1:37
    and it's not inside any function,
    so it just runs it,
  • 1:37 - 1:40
    and it only runs it once.
  • 1:40 - 1:44
    OK, let's turn this into a function.
  • 1:44 - 1:46
    To do that, we do it
    very similarly to the way
  • 1:46 - 1:48
    we declare a variable,
    because that's actually
  • 1:48 - 1:50
    what we are doing.
  • 1:50 - 1:52
    So we say 'var drawWinston'.
  • 1:52 - 1:55
    We give it a nice name,
    very descriptive,
  • 1:55 - 1:59
    and then '=', but here,
    instead of writing a number or a string,
  • 1:59 - 2:03
    we're going to write 'function'
    (make sure you spell it right)
  • 2:03 - 2:08
    and then empty parentheses '()'
    and then an open curly '{'
  • 2:08 - 2:12
    and then a close curly '}'
    and our semicolon ';'
  • 2:12 - 2:14
    OK so what we need to do
    is put everything
  • 2:14 - 2:19
    that we want inside our function
    in between the start and end curly.
  • 2:19 - 2:22
    So we're going to take
    all this code here,
  • 2:22 - 2:27
    put it in our function
    (indent it nicely), and Ta Da!
  • 2:27 - 2:29
    So now what we have
    is we have this variable
  • 2:29 - 2:32
    that is storing a function -
    so basically we've given
  • 2:32 - 2:36
    a label to this block of code,
    so that we'd be able to tell
  • 2:36 - 2:38
    our program at any time,
  • 2:38 - 2:41
    "Hey, find that block of code
    with that label and run it!"
  • 2:41 - 2:44
    We we're making
    this bit of code reusable.
  • 2:44 - 2:47
    But now notice, that we have
    no Winston anymore!
  • 2:47 - 2:49
    We've lost Winston!
    Where did he go?
  • 2:49 - 2:53
    OK - so what happened is
    that once we put this inside a function,
  • 2:53 - 2:56
    we told our program
    "hey here's a bunch of code
  • 2:56 - 2:58
    that I want to be able
    to run later,
  • 2:58 - 3:01
    but only when I TELL you to run it."
  • 3:01 - 3:04
    So we have to tell it to run the code,
    which means we need
  • 3:04 - 3:09
    to 'call' the function - just like we do
    with ellipse() and rect() and line().
  • 3:09 - 3:14
    So we just write
    the function name drawWinston
  • 3:14 - 3:16
    followed by our start
    and end parentheses '()'
  • 3:16 - 3:19
    and, of course, our semicolon,
    and Ta Da! --
  • 3:19 - 3:21
    We have a Winston!
  • 3:21 - 3:24
    OK! So I think it's cool,
    but you might not think it's cool
  • 3:24 - 3:27
    because all we've done is made
    our program do exactly
  • 3:27 - 3:30
    what it did before.
    Kinda silly huh?
  • 3:30 - 3:33
    The whole point of functions
    is we can reuse them.
  • 3:33 - 3:35
    So let's do that now.
  • 3:35 - 3:41
    We can just copy and paste this function
    call... Ta da! Ta da! Over and over.
  • 3:41 - 3:47
    Hmmm, but it looks the same,
    well, it worked -
  • 3:47 - 3:49
    it's drawing multiple Winstons,
    but the problem
  • 3:49 - 3:51
    is they're all in the same place.
  • 3:51 - 3:54
    If we had x-ray vision,
    we could x-ray the canvas
  • 3:54 - 3:58
    and see three Winstons,
    but I don't have x-ray vision.
  • 3:58 - 4:00
    I don't know about you.
  • 4:00 - 4:03
    But, we can make a small change
    to our function
  • 4:03 - 4:05
    that WILL make it obvious.
  • 4:05 - 4:09
    So you see faceX and faceY -
    they're always 202 and 208?
  • 4:09 - 4:12
    We can change this
    to using the random() function -
  • 4:12 - 4:15
    - let's say like random() from 50 to 350
    and it'll generate
  • 4:15 - 4:19
    a random number between those two -
    and we can do the same thing for here -
  • 4:19 - 4:23
    and so every time this function is called,
    it generates this new random number,
  • 4:23 - 4:27
    and if we push restart,
    we can keep getting random Winstons.
  • 4:27 - 4:29
    So cool!
    Whoo!!
  • 4:29 - 4:32
    Alright - so I think this is cool
    because it would have been
  • 4:32 - 4:35
    a lot of code to write this
    if we didn't have it in a function.
  • 4:35 - 4:38
    It would have been
    three times the amount of code.
  • 4:38 - 4:41
    But it's still not AS useful
    as it could be,
  • 4:41 - 4:44
    because we probably
    don't want random Winstons.
  • 4:44 - 4:46
    We probably want to be able
    to position a Winston
  • 4:46 - 4:48
    at specific points on the screen.
  • 4:48 - 4:50
    So stay tuned, 'cause we'll talk
    about how pass parameters
  • 4:50 - 4:52
    to our functions next,
    and then you'll be able
  • 4:52 - 4:56
    to do exactly that.
Title:
Functions
Description:

more » « less
Video Language:
Turkish
Duration:
04:56

English subtitles

Revisions