Logo (programming Language) - Syntax - Functions and Procedures

Functions and Procedures

Each line is made up of function calls, or subroutines in programming terminology, of which there are two types:

  • commands (which do something—effects—but do not return a value) like print.
  • operations (which just return a value, its output) like sum, first or readlist.

A command is similar to a Pascal procedure, and an operation is similar to a Pascal function. (See also: command-query separation, where a query is an operation in Logo). A special subset of operations, called predicates, which just output the word true or false, are conventionally written with a final p. Examples include emptyp, wordp, and listp.

  • Expressions can be primitives, or can be defined by the user.
  • Expressions can take zero, one or more parameters.

Procedures can be defined on the command line, using the TO END pair:

TO CHAIR REPEAT 4 FD 200 END

However, in some early Logos the procedure is limited to the physical line length of the input device.

All Logos can invoke an Editor, usually by EDALL. In the editor, procedures may be written over many lines, as nothing is interpreted until the edit is complete.

EDALL TO CHAIR REPEAT 4 FD 200 END

The new word is saved into the available vocabulary, but the definition will be lost once the Logo session is over. Internally procedures are words and in this case, any time CHAIR is entered, the sequence REPEAT 4 FD 200 will be executed. The word CHAIR can be used as a command; for example, REPEAT 4 would repeat the CHAIR operation four times.

EDALL ;(to enter the editor mode, then the actual procedure) TO ERASECHAIR PE BK 200 REPEAT 5 PPT END CS CHAIR WAIT 200 ERASECHAIR

A WAIT delay between the drawing and the erasing can introduce the illusion of motion:

CS REPEAT 20

Read more about this topic:  Logo (programming Language), Syntax

Famous quotes containing the words functions and/or procedures:

    Those things which now most engage the attention of men, as politics and the daily routine, are, it is true, vital functions of human society, but should be unconsciously performed, like the corresponding functions of the physical body.
    Henry David Thoreau (1817–1862)

    Young children learn in a different manner from that of older children and adults, yet we can teach them many things if we adapt our materials and mode of instruction to their level of ability. But we miseducate young children when we assume that their learning abilities are comparable to those of older children and that they can be taught with materials and with the same instructional procedures appropriate to school-age children.
    David Elkind (20th century)