Logo - Procedures
Procedures provide a way to encapsulate a collection of commands. Once a procedure has been created, it can be used just the way a built-in command is used. The “meaning” of a procedure is the meaning of its individual commands.
A procedure without arguments has the word ‘to’ (a reserved word) and the name of the procedure on the first line. (Reserved words in Logo cannot be used as variables and have a well-defined meaning and use.) It has the reserved word ‘end’ on the last line.
A subprogram is a named sequence of steps for another program to execute. Other names for subprograms are procedures and functions. In Logo, you tell the computer how to do something — for example −
to square repeat 4 [fd 100 rt 90] end
Once we have described our procedure to Logo, we can enter its name on the command line, just as we would do to any of the built-in things. In this case, we would type ‘square’ on the command line and Logo looks up the commands to make a square.
Click the button that says Edall (for edit all) to bring up Logo's built-in editor. (If your Logo doesn't have an Edall button, type ‘edall’ on the command line). The following code block has the required structure of the subprogram.
to procedurename steps of your procedure here end
The procedure or subprogram must start with the word ‘to’, followed by a name we think of. The next step is to key-in all the same steps we would write on the command line. The procedure must end with the word ‘end’. All comment or remark lines should be preceded by semi-colon (;).
Following is the practical demonstration of the above example −
Now, from the command line, execute the procedure using its name “square” as shown below −
Procedures can not only contain built-in commands, but they can also contain other procedures.
In the following example, a procedure ‘flower’ is calling our predefined procedure ‘square’ from its body.
Following screenshot shows the output when the procedure “flower” is called −
We don't want every square to be of the same size — we want variety. In Logo, we create variables, whose values we can change. We will use the same square procedure with a small change in the following example.
to square :n repeat 4 [fd :n rt 90] end
We give Logo a replacement value for ‘:n’ on the command line as shown below.
square 50 square 75 square 100
Here is the practical demonstration of the above example −
Now let us discuss how to pass two arguments to a procedure. Following screenshot is a practical demonstration of the same.