Reference

Video Lessons

Learning Objectives

Procedures

What are procedures?

We can talk about procedures (commands and reporters) in a couple of ways:

  • There are procedure names that use when you write an instruction. For example, in the instruction fd 50, we use the procedure name fd to tell Foo that we want to move forward. Foo invokes the procedure when he gets the input 50.
  • There are procedure definitions that tell Foo what to do when he sees that procedure in an instruction. The procedure definition tells Foo whether to look for inputs, how many inputs to look for, and what to do after the needed inputs (if any) have been collected.

Some MicroWorlds procedures are written by programmers at LCSI (Logo Computer Systems, Inc.). Programmers like you can add new procedures by writing them in the public Procedures tab or the private Procedures tab in a turtles backpack. The procedures written by LCSI are already known by Foo when you get your copy of MicroWorlds. These procedures are called primitives. You can imagine an invisible Procedures tab somewhere behind the screen where all of the definitions for all of the primitives are written.

Not everyone explains primitive procedures in this way. To understand why a correct definition is important, read this quote from Brian Harvey:

Some writers and teachers reserve the word "procedure" to refer only to ones you write yourself, such as hello. They use the word "primitive" as a noun, to mean things like print and butfirst. They say things like "Logo instructions are made up of procedures and primitives." This is a big mistake. The procedures you write are just like the procedures Logo happens to know about in the first place. It's just that somebody else wrote the primitive procedures. But you use your own procedures in exactly the same way that you use primitive procedures: you type the name of the procedure and Logo evaluates that name by invoking the procedure. It's okay to say "Last is a primitive" as an abbreviation for "Last is a primitive procedure," as long as you know what you're talking about.

Brian Harvey, Computer Science Logo Style, Volume 1, page 32

Where can you use procedures that you write?

A procedure that you write works just the same way as a primitive. You can write a procedure that needs zero or more inputs. You can write a procedure that is a command or a reporter. Once your procedure is defined, you can use it anywhere you would use a primitive:

  • in the Command Center
  • in the Rules tab of a turtle's backpack
  • in a button
  • in the definition of another procedure

How do you write a procedure?

(This section is taken from MicroWorlds EX Help > Programming > Programming Environment > About Procedures --TJ)

A procedure has three parts:

to square ; The title line "to" and the name of the procedure.
fd 50 rt 90 ; Instructions.
fd 50 rt 90
fd 50 rt 90
fd 50 rt 90
end ; The last line.

All procedures must start with to and the name of the procedure. You choose the name, but always make sure that it doesn't contain any spaces. The name must be unique in the Procedures Tab in which the procedure is defined and it cannot be a name that exists in the MicroWorlds EX built-in vocabulary (primitives, object name, page name, etc.). Procedures must also end with the keyword end on its own line.

  • The title line of a procedure (to the name, and its inputs) must be on a line by itself. You must press Enter at the end of the line. See below an example of a procedure with an input.
  • Lines of instructions within the procedure are separated by carriage returns (Enter), although they do not have to be. Separating them by carriage returns makes the procedure easier to read and debug.
  • The last line of a procedure is the word end, and it must be on a line by itself. Always press Enter after the word end.
  • You can insert spaces between your procedures to make them easier to read and understand.
  • You can put comments between your procedures. Simply never start a comment with the word "to".

What does Foo do when he sees the name of my procedure in an instruction?

Let's consider the procedure square from the example above. To follow along, you can write the square procedure in the Procedures tab of your project. The square procedure is a command that doesn't need any inputs. We'll talk later about how to write reporters and how to write procedures (commands or reporters) that need inputs.

Once the square a command has been defined, you can write it at the beginning of a line of instructions. To see the trail that the turtle travels, you can use pd to put the turtle's pen down. Try running these instructions in the Command Center:

pd square

What happened? First, Foo reads pd. He does this by collecting one character at a time until he sees a space. Since pd is a primitive (a procedure written by the folks at LCSI), Foo follows the directions in the hidden (LCSI) procedures tab to collect inputs (in this case, there aren't any) and do what he has been asked (put down the turtle's pen). Next, Foo reads square. Since square is a procedure written by you, Foo follows the directions in Procedures tab you can open in the tabs section. The directions don't say anything about inputs, so Foo invokes the square procedure write away. He starts with the first line of instructions (fd 50 rt 90) and continues running instructions until he get to the end.

So, if you use the definition of square that is written above, then pd square does the same work as:

pd
fd 50 rt 90
fd 50 rt 90
fd 50 rt 90
fd 50 rt 90

About the Center for Talent Development

Center for Talent Development (CTD), housed at Northwestern University's School of Education and Social Policy, is an accredited learning center and research facility that identifies, educates and supports gifted students and their families and serves as a leader in gifted education. Learn more about the Center for Talent Development.