Colon definition
    Il "Quarto" fuso ] The 4th Time Zone ]   


 

Introduction
Input/Output operations
Mathematical Operators
Stack operators
Other operators
Colon definition
Control structures
Conditional branching
Constants and variables
Other Forth words
Using the editor
Error messages


                                   

[ Index ][ Back ][ Next ]

These are the most powerful and most used forms of data structures in Forth, and are so called because they begin with a colon “:”.

Colon definition allow the creation of new Forth words based on previously defined words. They can be of any length, although carriage return must be pressed before a particular section.

The general format is:

:  new-word  word1  word2  ...  wordn  ;

all colon definition end with a semicolon.

If a word used in a colon definition has not been previously defined, then an error will result.

The new-word is executed simply by typing its name and pressing ENTER. e.g. suppose we wish to define a new-word to calculate the square of a given number, we could do this by:

: SQUARE  <CR>
DECIMAL CR ." THE SQUARE OF "  <CR>
DUP . ." IS " DUP * . ;  <CR>

Where we have defined a new word called SQUARE which will be called by

SQUARE <CR>

e.g.    9 SQUARE  <CR>

will result in

THE SQUARE OF 9 IS 81

If we follow the operations of the word we will see the changes in the stack

TOS

 

Operation

Result

Emtpy

 

 

 

9

 

9 SQUARE

 

9

 

CR

Carriage return

9

 

."

THE SQUARE OF

9 , 9

 

DUP

 

9

 

.

9

9

 

."

IS

9 , 9

 

DUP

 

81

 

*

 

Empty

 

.

81

We are now going to discuss control structures. It must be remembered that the control structures can only be incorporated in colon definition, or an error will result.

[ Index ][ Back ][ Next ]


Ultimo aggiornamento: 10-12-2002. Copyright (c) 2000-2002 Matteo Vitturi. 
Per problemi o domande relativamente a questo sito contattare il webmaster
Last update: 12.10.2002. Copyright (c) 2000-2002 Matteo Vitturi.
For problems or questions related to this web please contact the webmaster.
1