Reference

Video Lessons

Learning Objectives

Writing Procedures with Inputs

From Procedures with Inputs in MicroWorlds EX Help:

The square procedure draws exactly the same square each time it runs. The house procedure draws exactly the same house. Contrast this with the primitive forward.

forward 100

...draws a line twice as long as

forward 50

Since the forward command takes an input, you can use this command to draw lines of all different lengths.

If MicroWorlds expects an input for a primitive, it complains if you do not include one:

forward
forward needs more inputs

Procedures that you define can also have inputs. The square procedure above can be modified so that instead of drawing a square with side 100 each time, it can draw squares of all different sizes.

to square :side
repeat 4 [forward :side right 90]
end

Now each time that you want to run the procedure square, you must specify the length of the side of the square to be drawn. You can run it just like any MicroWorlds command that takes an input. For example, to make a square with sides of 50 steps, type

pd
square 50

square

To make a tiny square, type:

square 10

tiny square

Drag the turtle away to see the square.

The definition of square illustrates a general rule about defining a procedure with inputs. The name of the input must be written on the title line after the name of the procedure. You can choose any name for the input, but it must always be preceded by a colon. You can then use the input name (preceded by the colon) wherever you would want to use the value of that input in the procedure.

The title line of square tells MicroWorlds that the procedure square has a single input named side. The body of the procedure uses the primitive forward which has the input side. This instruction draws a line of the length :side.

When a procedure is defined with an input, that input does not have a value. It is only when the procedure is run that the value of the input is known. For this reason, the input must always be given a name.

You can define MicroWorlds procedures with more than one input. An example of a procedure that needs two inputs is a rectangle:

to rectangle :height :width
repeat 2 [forward :height right 90 forward :width right 90]
end

To run rectangle. you must type the name followed by two input values:

rectangle 50 100

rectangle

Here is another example using a superprocedure and subprocedures. We can change the house definition above so that it can draw houses of different sizes:

to house :size
square :size
forward :size
right 30
triangle :size
end

Triangle. then, should be defined as follows:

to triangle :side
repeat 3 [forward :side right 60]
end

To draw a house of size 150, type

house 150

Notice that even though square was defined with the input named side, it still runs inside of house with the input named size. The name of the input is just a container waiting for a value.

In the above example, how does MicroWorlds know that square, forward, and triangle all have 150 as their input. 150 is the "thing called" size.

When MicroWorlds sees the instruction for square, it looks for the definition of square and runs that procedure with 150 as its input.

Then it goes on to forward with 150 as its input, and it runs right 30. When it reaches triangle. it looks for the definition oftriangle and runs that procedure, again with 150 as its input.

Then it goes on to the next instruction in house which is end. Now house is finished.

When you give the superprocedure an input, it can pass the input to the subprocedures.

One way to explain how procedures can pass inputs to each other while still maintaining their own input values, is to think about inputs as names, with each procedure having its own private library of names (Abelson. 1982). Since the names of the inputs are private, different procedures can use the same name for an input without any confusion about its value. When MicroWorlds runs a procedure, the procedure sets up a private library which has the input names from the procedure definition associated with the actual values given at the time of the procedure call. When the procedure runs an instruction containing the input name, it looks up the value for the name in its private library. It is therefore possible to have the same name in two separate private libraries containing different information!

In computer science terminology, inputs are referred to as "local variables" of the procedures in which they are defined. The importance of private input names is that you can run a procedure without any concern for the details of precisely how it is defined. You can concentrate on what it does. When you define the house procedure, you can think of square as a "black box" that draws a square, without worrying about the name it uses for its input.

Abelson, H. (1982). Apple MicroWorlds. Peterborough, NH: Byte Publications Inc.

 

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.