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