Procedure Commands

Procedures are a way to package up instructions for individual characters in StarLogo. Procedures begin with the word `to` followed by the name you wish to give the procedure. After the name, you may enter any number of procedure parameters. Each parameter must start with a `:` (colon) to indicate that it is a local variable. After pressing return, you then enter all of the instructions you wish to run when the procedure is called. Finally, a procedure finishes with the word `end` on a line by itself. Here is an example:

`to wiggle
` `right random 40
` `left random 40
` `forward 1
` `end`

In this procedure, named `wiggle`, we instruct a turtle to turn right a few degrees, then left a few degrees and finally move forward one step.

Here is an example of a procedure with one parameter:

`to wiggle-a-little-bit :howmuch
right random :howmuch
left random :howmuch
forward 1
end`

In this procedure, named `wiggle-a-little-bit`, we take a parameter named `:howmuch`, which we use like any other variable to control by how much the turtle should turn right and left. We can run a procedure that takes a set of parameters by writing the procedure's name followed by the values that we want the parameters to have (in left-to-right order):

`to go
wiggle-a-little-bit 45
look-for-gold
end`

In this procedure, named `go`, we invoke the `wiggle-a-little-bit` function with the value`45`. 45 is assigned to the procedure parameter `:howmuch`, and then the procedure is run. Finally, when `wiggle-a-little-bit` finishes, we come back to where we left off in `go`, and then execute another procedure (not described here) called `look-for-gold`.

Note the difference between Turtle Procedures and Observer Procedures. Turtle procedures are instructions for individual turtles to follow, such as moving, or reproducing. Observer procedures are procedures that may change the environment or make changes which are beyond an individual turtle's control, such as the creation of new turtles or the setting up of the Patch Canvas. Think of the Observer as an omniscient being who can make changes to the StarLogo environment.

Observer procedures must be defined in the Observer Procedures Pane of the Control Center. Turtle procedures are defined in the Turtle Procedures Pane of the Control Center. If you put a procedure in the wrong place (such as putting wiggle into the observer procedures pane), you might get an error like `observer doesn't know how to right in wiggle`, indicating that the observer does not understand the command `right` used in the first line of wiggle.

While patches can run instructions, they cannot have procedures; there is no patch procedures pane. If you want to use patch commands, you must put them in an `ask-patches []` command. Since `ask-patches` is itself an observer command, it should be written in the observer procedures pane.

One special Observer Procedure is the `startup` procedure. If you name one of your Observer Procedures `startup`, this procedure will automatically be run when you load the project.

A / B / C / D / E
end
F / G / H / I / J / K / L / M / N / O
output
P / Q / R / S
startup stop
T
to
U / V / W / X / Y / Z