Inputs to Procedures

Like primitives, procedures can require information or input in the form of Logo objects. This allows a single procedure to operate in different ways depending on what information is supplied. For example, the Logo primitive FORWARD requires a number as input to specify how far to move the turtle.

You can re-write the SQUARE procedure illustrated above to also require an input to specify how large a square to draw. Do so by inserting a variable name in the title of the procedure and substituting the variable name for the distance the turtle is to travel to draw the side of the square. Remember that in Logo a variable name begins with a colon or dots.

? TO SQUARE :SIDE

> REPEAT 4 [FD :SIDE RT 90]

> END

SQUARE defined

? SQUARE

SQUARE needs more input(s).

? SQUARE 100

When you insert a variable name in the title of a Logo procedure, you indicate to Logo that information must be supplied along with the procedure name when the procedure is invoked. The first SQUARE procedure illustrated above only draws squares of size 100. The second SQUARE procedure draws squares of any size, but you must specify the size when you call the procedure.

Optional Inputs

You can also create Logo procedures whose inputs are optional, meaning that the procedure will operate with a default or pre-specified value if none is given or with the supplied value if one is available. Procedures with optional variables have a specific form:

? TO SQUARE [:SIDE 100]

> REPEAT 4 [FD :SIDE RT 90]

> END

SQUARE defined

? SQUARE

? (SQUARE 50)

Optional variables in procedures and their default values are enclosed in brackets and made part of the title line in the procedure definition. If the procedure is called with no argument, it operates with the default value of the variable. When a value for the optional variable is supplied, the procedure name and its inputs(s) must be enclosed in parentheses to indicate to Logo that optional information is included.



If a procedure has both required and optional variables, the optional variables must be specified in the title line after the required variables.

? TO SQUARE :PEN.WIDTH [:SIDE 100]

> SETWIDTH :PEN.WIDTH

> REPEAT 4 [FD :SIDE RT 90]

> END

SQUARE defined

? SQUARE 3

? (SQUARE 3 50)

If you want to specify the number of default arguments, add this number to the list of arguments. This requires the user to supply the specified number of arguments, unless parentheses are used.

? TO POWER :M \

? [:N 2] 2

>   LOCAL "RES

>   MAKE "RES :M

>   REPEAT :N-1 \

> [MAKE \

> "RES :RES * :M]

> OUTPUT :RES

> END

POWER defined.

? POWER 2 3

Result: 8

? POWER 2

The procedure POWER needs more input(s).

? (POWER 2)

Result: 4

?

List arguments

You can also write procedures which accept any number of inputs. When such a procedure is called, all inputs are passed to the procedure in the form of a Logo list when it can evaluate. to create such a procedure. The following procedure prints its arguments one by one:

? TO PRINT.INPUTS [:LIST]

>    WHILE [NOT EMPTY? :LIST] [PR FIRST :LIST MAKE "LIST BF :LIST]

> END

PRINT.INPUTS defined.

? PRINT.INPUTS 1 2 3

1

2

3

?

You can also specify the number of required arguments. This is convenient since Logo would add everything following the name of the procedure to the list passed to the procedure as input.

? PRINT.INPUTS 1 2 PRINT "FINISHED 3

FINISHED

1

2

3

?

In this example Logo evaluates all arguments on the input line following the procedure name, since PRINT.INPUTS does not have a required number of arguments. First PRINT was evaluated, printing the word FINISHED. After this, the input list for PRINT.INPUTS was constructed, leaving an empty word between the numbers 2 and 3 because PRINT does not output anything. Finally, the list was printed by PRINT.INPUTS. The following example illstruates how to specify the number of inputs.

? TO PRINT.INPUTS [:LIST] 2

>    WHILE [NOT EMPTY? :LIST] [PR FIRST :LIST MAKE "LIST BF :LIST]

> END

PRINT.INPUTS redefined.

? PRINT.INPUTS 1 2 PRINT "FINISHED 3

1

2

FINISHED

Result: 3

? PRINT.INPUTS 1

The procedure PRINT.INPUTS needs more input(s).

?