Section 17.5 Parameters
The formal name for an input to a function is
parameter:
One of the trickiest parts of learning to write and use procedures and functions is developing an accurate understanding of how parameters work. Think of parameters as variables in a procedure that are set by the procedure call. Just like any variable, parameters can hold any value. And, just like any other variable, when you use the name of a parameter, you are accessing the value that the parameter is holding.
When we call (use) a procedure, we must provide values for each of the parameters that it has. The values that we provide are known as the
arguments.
When this procedure call happens, all the code from
hop
will be executed. Any time we encounter the parameter
turtleName
, we will use the argument -
sue
in its place. This process of giving the procedure information by specifying arguments is known as
passing parameters to the procedure.
This system allows the procedure to work with any turtle. We can call
hop(buster)
to run
hop
and when we do so,
turtleName
will mean the turtle named
buster
.
Try running this program below. It defines the
hop
procedure. Then, the main part of the program makes two turtles, has them mark their starting location, and then has them each do a hop before doing a forward movement.
Checkpoint 17.5.3.
Which turtle made the line going North (up)?
Checkpoint 17.5.4.
This code sample makes a turtle called ray and then calls the spin procedure. What belongs in the blank to make the program work?
def spin(who):
________.left(180)
from turtle import *
ray = Turtle()
spin(ray)
ray
ray is the argument. To talk about the argument inside the procedure, we have to use the parameter’s name.
who
Correct. Inside this procedure, who will be the name we use for whatever turtle is the argument in the function call.
turtleName
The parameter for this function is not called turtleName.
You have attempted
1 of
3 activities on this page.