6.1. Procedures that Call Procedures¶
Once we start writing procedures, we may want to make use of our new abstractions to build
even more complex new procedures. We have actually already used this idea - our procedures
like square make use of existing turtle procedures like forward. But we can also
use our own existing procedures to write other new procedures.
Say we want to draw this shape:
We notice that it consists of three rows. Each row consists of three squares. We already know how to write a procedure draw a square… lets use it to write a procedure to draw a row of squares:
The row procedure has one parameter - turtleName. Whatever turtle is specified as the
argument in the procedure call to row will be called turtleName inside row. So
because we call row(roger), turtleName will mean roger.
Inside the row procedure, we call the square procedure: square(turtleName, 10).
Because turtleName refers to roger at this point, that is the same as
square(roger, 10). Because the first parameter of square is turtle, that means
any time we say turtle in the square procedure, we are going to be working with
roger.
Now we can use the row to help write a grid procedure that makes the final picture:
Our grid procedure, the parameter is called turtle. Which is the same name that square
uses for its first parameter. It is OK for two different procedures to use the same name for
their parameters. It can however be confusing… just because they have the same name, does not
mean that they are always going to have the same value. Just like two people who both make a
phone call to “Bob” may be calling the same person, or may not be.
In this case, roger is passed to grid, which calls the turtle turtle, and passes
it to row, which calls it turtleName, which passes it to square which calls it
turtle. Each of the procedures has its own “nickname” for the turtle that they are all
working with.
The following code is supposed to draw a picture consisting of four “lines”; each “line” has three squares.
Put the blocks in the right order and indentation. As usual, you will not use all the blocks.
