BFOIT - Introduction to Computer Programming

Defining Procedures in the CommandCenter

Although most of the time you will use TG's Editor to enter procedure definitions, you may on occasion want to define one that you don't want included in program you are composing in the Editor.  Let's walk through defining a new procedure in the CommandCenter.

First of all, when you enter a procedure definition into the CommandCenter, it behavior is a little different.  Most of the time when you type instructions into the CommandCenter, something happens when you press the [Enter] key.  As an example, When you type "fd 100" into the CommandCenter and press [Enter], the turtle moves forward in the graphics canvas.  But, when you type in the title line of a procedure definition and press [Enter], the way TG responds is different.  As Figure 4a.1 shows, the '?' prompt character you've been seeing has changed to '>' and the cursor is indented a couple of spaces.

CmdCtr DefCmd Figure 4a.1

And now as you type in the lines of instructions that make up the body of the new procedure, nothing happens in the graphics canvas.  Instead, TG keeps responding to your pressing the [Enter] key with another '>' prompt character and with the cursor indented.  And, it keeps doing this until you enter the end line. 

Figure 4a.2 shows TG following the entering of the end line.  All is back to normal. 

CmdCtr DefCmd Figure 4a.2

Try this out yourself - now...

Your browser does not support Java applets. Click here for more information. TG Programming Environment Applet

Once you've completed the definition of the procedure you can use it like any of the Logo commands you've been using.  You've just taught TG's Logo interpreter how to do something; you've added a new command to Logo's vocabulary.

Type the new procedure's name (drawO) into the CommandCenter and press the [Enter] key.  Does it work?

The CommandCenter is great for adding a trivial command when you just want to experiment.  But, once you've entered a line of instructions, that's it; there's no going back to change them.  You must quit the current definition (either by entering an end line or by holding down the [Ctrl] key and pressing Q - the QUIT directive) and then start all over.  This is not too bad since you can use the up/down arrow keys to recall previously entered lines.  So, how do you know if you entered the procedure correctly?

When you type command definitions into the CommandCenter and make a mistake in the process, you don't always end up with what you think you should.

Here are a couple of directives that may help you with problems associated with user-defined procedures. 

TG Directive Description
Print a list of the user-defined procedures that TG's Logo interpreter knows about.
Print the text (the source code representation) of a specified user-defined procedure.
Table 4a.1

Click the mouse in the CommandCenter and type in the printprocs directive.  The name of your new procedure, drawO, should be printed.

Figure 4a.3 shows what you should see.

PRINTPROCS drawO Figure 4a.3

It not, you somehow messed up the definition.  Try typing the definition of drawO into the CommandCenter again.

If it is, next try typing in the printtext directive.  Figure 4a.4 shows what you should see.

PRINTTEXT drawO Figure 4a.4

PRINTTEXT asks the Logo interpreter in TG to print out the definition of the procedure whose name you give it.  Check out what it prints when you enter it with drawO as an input.  Does it match what you thought you entered?  If not, reenter the definition until it matches what one of the above examples looks like.

For the most part, you should use the Editor when programming.  But, now you know an alternative which is good.

Public Domain Mark
This work (BFOIT: Introduction to Computer Programming, by Guy M. Haas),
identified by Berkeley Foundation for Opportunities in IT (BFOIT),
is free of known copyright restrictions.