Colon Definitions


Next: COD Files Up: COD Previous: Interactive mode

Colon Definitions

When running COD interactively, it is sometimes necessary to enter the same sequence of tokens several times. For such cases, you should create a colon definition that contains the sequence. A colon definition consists of a colon : , followed by the function name, followed by the sequence of COD functions that you wish to execute when the function name is typed, and terminated with a semi-colon ; . For example, although there is no built-in COD function to square a number, you can create one with

COD> : X2 DUP * ;
which will have the effect of multiplying the top number on the stack by itself. After you have defined X2, it may be used in exactly the same way as any built-in function; thus,
COD> 3.0 X2
 9.0
A previously defined colon function may be used in the definition of a new colon function. An X3 function, for example, can be constructed from the X2 function with
COD> : X3 DUP X2 * ;
 9.0
COD> 3.0 X3
 9.0 27.0
The name of a colon function is not allowed to match the name of any built-in function or other colon function. Hence, a colon function cannot be used to redefine the action of any existing COD keyword.

It is possible to enter a multi-line colon definition interactively. While in the midst of a multi-line colon definition, the stack will not be printed just before the COD prompt. The following example shows one way to enter a colon definition that prints the integers from one to five:

COD> : COUNT5
COD>   5 1 FOR
COD>          I .
COD>       LOOP
COD> ;
 9.0 27.0
COD> COUNT5
 1.0
 2.0
 3.0
 4.0
 5.0
 9.0 27.0
The last line, just before the next COD prompt, is the stack. This allows us to verify that the original stack has been changed, and therefore, COUNT5 is not altering the stack.



Web Page Maintained by: Dr. Lawrence E. Brown elwin@redshift.gsfc.nasa.gov