< Return to Video

Functions with Parameters

  • 0:05 - 0:10
    Here's a function that draws a square with
    50 pixel sides. That's great but what if I
  • 0:10 - 0:16
    wanted to draw a square with sides 50 pixels
    and another square with sides of 100 pixels?
  • 0:16 - 0:21
    We shouldn't have to make two separate functions
    that do almost the same thing. Instead, we
  • 0:21 - 0:27
    can use one function with a parameter. Parameters
    allow us to pass values into a function that
  • 0:27 - 0:32
    get used as variables inside the function.
    Let's try adding a parameter called like to
  • 0:32 - 0:38
    this function so we can use it to create squares
    of different sizes. In the function editor,
  • 0:38 - 0:43
    you can edit the name and description like
    before but now you can also add a parameter.
  • 0:43 - 0:48
    Write the name of your parameter in the space
    and click Add Parameter. This will create
  • 0:48 - 0:54
    a red block with the name of the parameter
    you just created. Now we can replace the move
  • 0:54 - 1:02
    forward by 100 block with the parameter Length
    so it will move forward by Length. Click save
  • 1:02 - 1:08
    and close and drag out your new create a square
    block from the functions category in the toolbox.
  • 1:08 - 1:12
    You'll notice there's an empty space next
    to length because the function wants to know
  • 1:12 - 1:17
    what value the parameter Length should have.
    Drag a number block from the Math category
  • 1:17 - 1:22
    and place it in this space. See how you can
    use the function again and again with different
  • 1:22 - 1:26
    lengths to make squares of different sizes?
    Try it for yourself!
Title:
Functions with Parameters
Video Language:
English
Duration:
01:28

English subtitles

Revisions