How to Write the Square Procedure and Create a Button in MicroWorld EX

By Camille McCue

To learn how to write a square procedure in MicroWorld EX, as well as creating a button for this procedure, simply follow the following steps to create the new command.

  1. Click the project Procedures tab (located in the lower-right corner of the MicroWorlds EX window) to open the project Procedures pane.

  2. Type the following code for this example.

    image0.jpg

    A new procedure starts with to followed by the name of the procedure, in this case, square. Do not use spaces in the name of the procedure. Always end the code for your new procedure with end. The end command must be on its own line, so be sure to press Return/Enter after typing it.

    Procedure names may not have spaces. If you want to separate words in a procedure name, use the underscore symbol: wiggle_around. You can also use the dash symbol, but this is not recommended because the dash can be confused with a minus sign.

  3. Test your new procedure by typing square in the Command Center to check whether it works as expected.

    A common mistake among new programmers is leaving off the word to at the start of your procedure definition. If this occurs, the Command Center issues an error message shows up. If you see an error message, simply edit your procedure and test it again until it executes as expected.

    image1.jpg

  4. In the Procedure pane, edit your square procedure to appear as follows:

    image2.jpg

    A key principle of writing computer code is keeping your code efficient, meaning as simple as possible. Because the square procedure is executing the same command (fd 30 rt 90) four times, a more efficient way to write the code is to use the repeat command.

    Notice that the primitive repeat is followed by a number showing how many repetitions to perform, and then the repeated phrase is placed inside square brackets: [fd 30 rt 90].

  5. Test your revised procedure by typing square in the Command Center to check that it works as expected.

    It may be helpful to drag the turtle to a new location first so that the new square isn’t drawn on top of the previously drawn square.

  6. Create a button with the label Square and the instruction square.

  7. Drag the Square button to a position in the workspace where it doesn’t block the artwork the user will be making.

  8. Test the button a few times, moving the turtle to different locations as you draw squares, as demonstrated below.

    image3.jpg

Use the clean command (or button, if you made one!) to erase drawings from the workspace if it becomes too crowded.

A square can be constructed using four right turns or four left turns, so the square procedure could just as easily have used an lt instead of an rt command!