Before you keep reading...
Runestone Academy can only continue if we get support from individuals like you. As a student you are well aware of the high cost of textbooks. Our mission is to provide great books to you for free, but we ask that you consider a $10 donation, more if you can or less if $10 is a burden.
Before you keep reading...
Making great stuff takes time and $$. If you appreciate the book you are reading now and want to keep quality materials free for other students please consider a donation to Runestone Academy. We ask that you consider a $10 donation, but if you can give more thats great, if $10 is too much for your budget we would be happy with whatever you can afford as a show of support.
The formal name for an input to a function is parameter:
def hop(turtleName): # turtleName is a parameter ...
One of the trickiest parts to learning how 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.
... hop(sue) # a procedure call using the sue turtle as the argument
When this procedure call happens, all the code from
hop will be executed. Any time we
encounter the parameter
turtleName, we will actually 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 and when we do so,
turtleName will mean the turtle named
Some people call parameters the formal parameters and arguments the actual parameters. We will stick to parameters and arguments to avoid confusion about what we mean when we just say “parameters”.
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, then has them each
do a hop before doing a forward movement.
The only variables you can use in a procedure are its parameters or new ones you create inside the procedures. It is an error to use variables from the main part of the program while writing the body of a procedure.
Think of each procedure as a self contained world. All that is known inside that world is what is passed into it as parameters.
Check Your Understanding