How to Create a Procedure

A procedure, in StarLogo, contains a set of instructions or commands that an agent can call to execute within the program. Procedures are useful when you want to use a set of instructions multiple times or when you want to break down a complicated task into discrete parts and program each part separately in an easy-to-read way. This tutorial will show you how to create a simple procedure that makes agents in Spaceland move randomly.

Getting ready

When you start StarLogo TNG, the default is a new project. If you already have another project open, go to the File menu and select New Project.

Step 1: Drag out a "Procedure" block

The first step to creating a procedure is to drag out a "Procedure" block onto the canvas.

1. Click to open the Procedure drawer in the Factory palette.
2. Click on the "Procedure" block and drag it onto the canvas. For this tutorial, we will place it on the Turtles page.

Once the block is placed on the canvas, you will notice a change to the block. The name of the page on the canvas will appear at the top of the block and the name of the procedure at the bottom. Right now, our block is called "Procedure".

3. We can change its name by clicking on the Procedure text to select it and typing a new one. Let's call this procedure "Movement".

Step 2: Add movement blocks

We want this procedure to make turtle agents move randomly, so we will need to assemble some movement blocks to do this.

1. Click on the Movement drawer in the Factory palette.
2. Drag out "forward", "left", and "right" blocks onto the canvas and connect one block after the other under the procedure block.

Step 3: Alter the movement blocks

As you see, once these specific blocks are placed on the canvas, they come with number blocks as well.

1. Click the Math drawer, drag out two "random" blocks to a blank part of the Turtles page. The random blocks automatically come with a number block ("10"), but we will not need them for this procedure so we can delete the number blocks by dragging them to the trashcan or selecting them and pressing the delete button on your keyboard.
2. Detach the "90" blocks from the "left" and "right" blocks and attach them to the two "random" blocks. Finally attach the new random blocks to the two movement blocks.

Now our procedure is complete and we can program agents to call the procedure to execute the commands.

Step 4: Drag out a runtime block

Most procedures are directly used in runtime blocks. However, they can be also called anywhere, even within another procedure. For this tutorial, we will take the simple approach and call our procedure in a runtime block.

1. Go to the Runtime page of the Canvas.
2. Click the Setup and Run drawer in the Factory palette and drag out a "Forever" block.

Step 5: Call the procedure in the runtime block

When a procedure is created, a new block is also created with the same name of the procedure. This block can be used to call that procedure in any place we specify.

1. To find this new call-block we will go to the "My Blocks" palette by clicking the right arrow next to Factory.
2. Click on the Turtles drawer and find the block labeled "Movement". This is the call-block.
3. Drag the call-block onto the canvas and attach it to the Forever block underneath the Turtles hook.

Step 6: Run the procedure

When a "forever" block is placed on the canvas, a new button appears in the runtime area of Spaceland. Click "forever" and the default turtles will roam around the terrain!


Now that you've learned to define a specific procedure, here is a general rule that can be followed for making other procedures.

1. Define the procedure.
(Procedure declaration block is in the Procedure drawer)

2. Call the procedure.
(Procedure call block is in the My Blocks palette, in the drawer that corresponds to the page where procedure is defined)