< Return to Video

CS Principles: Functions with Parameters

  • 0:04 - 0:08
    We've already seen that some functions
    are defined with parameters
  • 0:08 - 0:10
    to make them more generally useful.
  • 0:11 - 0:14
    For example the original version
    of moveForward moved
  • 0:15 - 0:16
    the turtle exactly 25 pixels.
  • 0:17 - 0:20
    This doesn't give you a lot of options
    on how to design your drawings.
  • 0:21 - 0:25
    A much more useful version of moveForward
    is one that accepts a parameter that
  • 0:26 - 0:29
    allows you to specify exactly how far
    forward the turtle should move.
  • 0:30 - 0:35
    Up to this point, the functions we've
    written ourselves don't have parameters.
  • 0:35 - 0:41
    But the ability to write your own function
    with parameters is both useful,
    and powerful.
  • 0:42 - 0:46
    So, let's see how to do it.
    Here's an example.
  • 0:46 - 0:50
    Look at this function that draws a square
    with sides that are 100 pixels long.
  • 0:50 - 0:54
    If we wanted to make a function to draw
    a square with sides 50 pixels long,
  • 0:55 - 0:59
    we'd have to make a new function that's
    almost identical to the first one
  • 1:00 - 1:03
    with the only difference being how
    far the turtle is moving each time.
  • 1:04 - 1:08
    If we wanted even more options for the
    size of the square, every time we would
  • 1:09 - 1:11
    need to declare a new function.
  • 1:12 - 1:14
    Yeah that's not very good. But luckily,
    there's another way.
  • 1:15 - 1:17
    The solution to the problem is the
    ability to create a function
  • 1:18 - 1:20
    that accepts a parameter.
  • 1:20 - 1:23
    A parameter is a named value that is
    provided as input to a function.
  • 1:24 - 1:28
    We saw that our version of drawSquare
    has a lot of code that was repeated.
  • 1:28 - 1:32
    The turtle was executing the same basic
    behavior just moving by different amounts.
  • 1:33 - 1:37
    It turns out that we can express this
    generalized behavior in our code as well.
  • 1:38 - 1:40
    Here's a new version of drawSquare.
  • 1:40 - 1:44
    Here we've made the size of the square a
    parameter of the function. Notice that the
  • 1:45 - 1:47
    function heading declares
    that it needs an input.
  • 1:48 - 1:51
    We've given a name to that input
    and that name is "size".
  • 1:52 - 1:56
    Everywhere in the code where we need to
    use that value we enter the word "size"
  • 1:56 - 1:58
    instead of a number.
  • 1:58 - 2:01
    Writing a function with a parameter is
    very similar to writing a function
  • 2:01 - 2:05
    without one. Just drag the function
    block that shows a parameter
  • 2:05 - 2:09
    and drop it into the work space.
    We add the name of our parameter
  • 2:09 - 2:12
    inside the parentheses in the
    function call like this.
  • 2:13 - 2:16
    Just as giving a meaningful and
    descriptive name to your function
  • 2:16 - 2:19
    is important, so is deciding
    what to name a parameter.
  • 2:19 - 2:22
    The name of a parameter
    should give some incite
  • 2:22 - 2:25
    into what the value will be used for.
  • 2:25 - 2:28
    In this case, we've chosen the name "size"
    to indicate that you can set the size of
  • 2:28 - 2:30
    the square when calling the function.
  • 2:31 - 2:35
    As a programmer, what you call your
    parameter is your decision to make.
  • 2:36 - 2:38
    We can write the body of a function
    like this.
  • 2:39 - 2:42
    The parameter "size" acts like a place
    holder for that value.
  • 2:42 - 2:49
    Wherever we want to use that value we just
    refer to it as "size" instead of a number.
  • 2:52 - 2:54
    Let's look at what happens when the
    function is called.
  • 2:55 - 3:00
    Now when a user calls drawSquare
    they call it with a value like this.
  • 3:01 - 3:04
    We say the value of 63 gets "passed"
    to the parameter of the function.
  • 3:04 - 3:07
    The parameter acts like a container
    with a name on it.
  • 3:07 - 3:11
    Whatever value is passed into the
    function is stored in that container.
  • 3:12 - 3:14
    Inside the body of the function,
    you'll want to use the value
  • 3:14 - 3:16
    stored in the parameter.
  • 3:17 - 3:19
    To do so, you must refer to the value
    by its name.
  • 3:20 - 3:23
    As the program is running, whenever
    the word "size" in this case is
  • 3:23 - 3:27
    encountered, its replaced by the value
    stored in that parameter.
  • 3:28 - 3:32
    Adding a parameter to our drawSquare
    allows us to generalize its functionality.
  • 3:33 - 3:37
    In other words, our function is useful
    in an entire range of situations
  • 3:38 - 3:41
    as opposed to just being useful in
    one particular instance.
  • 3:42 - 3:45
    The result is a function that gives you
    much more flexibility and control
  • 3:45 - 3:49
    without the need to create
    duplicated code.
  • 3:49 - 3:52
    Once you've added a single parameter
    to your function, you might realize
  • 3:52 - 3:56
    that there are multiple aspects of its
    functionality you'd like to generalize.
  • 3:56 - 4:02
    For our square example, you might want
    to change the width or color of the line.
  • 4:02 - 4:06
    To do this, you can simply add more
    parameters and separate them by commas.
  • 4:07 - 4:12
    You can then use any of these parameter
    names in the body of your function.
  • 4:12 - 4:16
    When calling your function, you'll need
    to include multiple values.
  • 4:16 - 4:19
    One for each of the parameters, and
    each one separated by commas.
  • 4:20 - 4:24
    Parameters are another example of how
    abstraction can be used to solve problems.
  • 4:25 - 4:29
    When confronted with a group of related
    problems, we first work to identify
  • 4:29 - 4:33
    patterns across all of them. In doing so,
    you might realize that using parameters
  • 4:33 - 4:37
    will let you define a single function with
    general behavior that can solve any
  • 4:37 - 4:40
    instance of the problem
    with one piece of code.
  • 4:40 - 4:44
    This is a powerful skill that removes
    the need for creating duplicate code,
  • 4:45 - 4:49
    and leads to programs that are easier
    to read, write, and maintain.
Title:
CS Principles: Functions with Parameters
Description:

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

English subtitles

Revisions