Day 7 - Programming Procedures

MicroWorlds Version 2.0 for Windows 95
Today I would like to learn how to write procedures. Procedures are hunks of program code that can be run just by giving one word, namely the name of the procedure. For instance, at the end of the last day I had written a line of code (a series of instructions) that looked like this setc "red setpensize 20 pd fd 100 rt 90 fd 200. Suppose I wanted my turtle to do this several times. Rather than typing this over and over again, it would be nice if we could just type one word, and the turtle would do the whole series of instructions. Let's give it a try.

First I searched in the Help and found out that there is a "Procedures Page", so I select Pages, and then Procedures Page from the menu. In Help, I also learned that a Procedure starts with the word to and then the name of the procedure. So on the Procedures Page, here is what I type to doit setc "red setpensize 20 pd fd 100 rt 90 fd 200 end Then using the shortcut, Ctrl F, I switch back to the main page. In the Command Center (the bottom of the page) I type doit and then doit again. Neat, it works! Here is what it looks like

 
I am going to Save this program, so I go File Save on the menu. MicroWorlds, as most other programs, suggests saving the project in the same directory as the program, but I have painfully learned after many years, that this is a bad idea. On my computers I always have a directory called _work and then many sub directories off of it. The purpose of the underscore in the front is so that it is always at the top of the list when the directories are sorted alphabetically. One of the advantages of keeping the work separate from the application is that in a backup, typically it is the work you want to save, not the whole big application, and this makes it much easier to back up all your work from every application. Another advantage is that when you upgrade an application, sometimes it is nice to install it to a new directory and keep the old version on your hard drive in its original working state for a while until you are sure the new version can read all your old files and that you like it. When you decide to delete the old version, you don't want to delete your work with it!

So now that we have a start on procedures, let's take a look at one more nifty MicroWorlds Logo command repeat. I would like to have my turtle draw a box. First, let's modify the procedure so that it draws one side and then turns the corner. Go to the procedures page and modify the code to look like this.

to doOneSide
   setc "red
   setpensize 10
   pd fd 200
   rt 90
end
The Ctrl-F back to the main page and use the cg and cc commands to get a nice clean place to work. In the command center type doOneSide. We get a straight line and the turtle turns. On mine, the turtle gets pretty close to the top of the screen, so lets use cg to clean the screen and drag the turtle, with your mouse, to the bottom left quarter of the screen. In the command center I will type repeat 4 doOneSide Nuts, it didn't work! See, that's what happens when you share your learning experiences over the Web. You can tell that this is a 'live' report as compared to a 'canned' tutorial. So, I went to the Help again and search on 'repeat'. Skimming through an animation example, I see repeat used with square brackets around the instructions to be repeated.

Let's give it a try. I type I will type repeat 4 [doOneSide]. It works! The procedure is called 4 times and we get a box.

 
So, it seems that repeat needs to be told 2 things, the first one is a number and the second one is [what to repeat], which must be in brackets.

Well we accomplished quite a bit today. You are seeing how one person, who chooses not to follow the tutorial that comes with the program can learn how to program in Logo. I hope you have enjoyed these 'shared days'. They were intended to give you a feel for what MicroWorlds is all about. This will be the last of the 'Days', because I think they have accomplished their task.

My next task is to develop some Logo lessons and share them on the web. Keep an eye out for an announcement about a new web site. If you would like your child to learn to write computer programs (or maybe you!), to really learn to control the computer, I highly recommend MicroWorlds!