![]() |
|||
Programming in Direct ModeLOGO allows the keyboard input and execution of commands in direct mode. Direct mode means that each time you press the return key, the command or commands that you have typed are executed. Working in direct mode is a good way to explore the early stages of learning to program and is also a way to work out the details of sophisticated programs after you have become more proficient. Try some of the individual commands (primitives). If you type the commands, you should see the turtle move as indicated. By the way, if you don’t want the effect of your commands to accumulate on the screen, enter CG (cleargraphics) at any point to clear the display. Now, instead of individual commands, try entering the following sequence of commands before pressing the return key: FD 30 RT 90 FD 30 RT 90 FD 30 RT 90 FD 30 RT 90 . Square resulting from LOGO commands
Programming in Indirect Mode: Writing ProceduresWhen working in indirect mode, the programmer constructs a set of instructions to be stored for execution at a later time. Because the programmer has to anticipate mentally the effect of each command, this approach makes some new demands. Often the programmer makes an error in thinking through what will happen when the entire sequence of commands is executed, and unexpected consequences occur (computer programmers refer to the errors in their programs as bugs). One very nice feature about a stored program is that if the programmer can determine the cause of the flaw in the program, he or she can locate the bug in the stored program and change the offending command or commands. In a way, this is similar to using a word processing program to edit a written document. Editing requires the writer to correct only the offending statement, not redo the entire document. In LOGO, a set of stored instructions is called a procedure. The general format for a procedure is:
To generate a square as a procedure, you might enter the following list of commands:
In direct mode, the entire sequence of commands can now be executed by simply entering SQUARE (then press Return). The procedure SQUARE has been added to the LOGO vocabulary. In a way, the programmer increases the power of LOGO by contributing to what LOGO knows how to do. This is what we mean by describing programming as "instructing the computer." What if you made a mistake–say, you incorrectly entered 3 instead of 30 for one of the sides? The figure drawn by the procedure would not be what you anticipated. Flawed square procedure
However, the process of correcting the error is relatively easy. You simply return to indirect mode, examine the procedure to find the error, and make the necessary correction. You can now execute the procedure again. If your modification fixes the actual bug, your program should execute as intended. Additional Features of LOGOThe procedure SQUARE is written in a manner that is not particularly elegant because the two primitives FD and RT are repeated over and over again. What if the procedure involved some cyclical process that had to be repeated hundreds or thousands of times? It would be rather impractical to enter any set of commands that many times. The command REPEAT number [commands] tells LOGO to repeat the commands within brackets the designated number of times. You could rewrite the procedure SQUARE as follows:
Powerful programming languages also make use of variables. The value of a procedure such as SQUARE would be limited if you had to rewrite the procedure every time you wanted the turtle to draw a square of a different size. In a way, variables allow you to establish the sequence of actions to be taken when the program is written and to defer assigning specific arguments to these actions. To write a more flexible version of the square procedure, you could establish the length of a side when the procedure is actually executed. You could write this more flexible version of the square procedure this way:
Once you have created the square procedure, you execute the procedure by entering SQUARE followed by the length of the side you want (for example, SQUARE 40). If you forget and enter only the procedure name, LOGO will ask you for the missing value. The way LOGO designates and uses variables might be a bit confusing at first. SIDE is a variable name. When a variable name is preceded by a colon (as in :SIDE), this means the value of a variable. When the REPEAT command encounters FD :SIDE, it substitutes the value of SIDE. To establish the value of a variable, you can also use the primitive MAKE "variablename value (as in MAKE "SIDE 40). Instead of entering SQUARE 40 to draw a square 40 turtlesteps on a side, you could also enter the command MAKE "SIDE 40 and then the command SQUARE :SIDE to accomplish the same thing. To keep these different ideas straight, you might think of a variable name as a storage place, like a box somewhere in the computer’s memory. The MAKE command attaches the variable name to a storage location and allows you to put information in this storage place. When the value of a variable is requested (as in :SIDE), LOGO will check the designated storage location to obtain the needed information. If LOGO finds an empty storage location, LOGO will generate an error message and ask for the needed information (SQUARE NEEDS MORE INPUTS). |
|||
| About | Outline | Copyright | |||