How to Write Procedures in MicroWorlds EX
Procedures combine primitives and other procedures to create new commands. Procedures are written in the Procedures pane of the MicroWorlds EX interface. A procedure begins with the word to and a one-name word for the procedure. It ends with the word end, which must be on its own line. The procedure can then be executed by typing the procedure name in the Command Center, or by including it in another procedure or a button instruction or anywhere a command is used. Following are some examples of procedures.
The following code defines a procedure named redsquare that draws a red square of side length 50:
to redsquare setc "red pd repeat 4 [fd 50 rt 90] end
The following code assumes that there are two text boxes: one named heads and one named tails. This code defines a procedure named initialize that sets the value of the heads variable to 0:
to initialize setheads 0 settails 0 end
The following code assumes that there are two shapes: one named heads and one named tails. This code defines a procedure named cointoss that sets the value of the flip variable to a randomly generated number (either 0 or 1); it then shows a heads shape or a tails shape according to the outcome:
to cointoss setflip random 2 if flip = 0 [setsh "heads] if flip = 1 [setsh "tails] end