The last thing that must be discussed in this brief introduction is how data are handled. The word : (colon) is called a defining word because it is used to add new words to the dictionary. There are other defining words that come with a Forth system, and Forth provides a mechanism for the user to make others. The defining words for data items create new words (children) in the dictionary and allocate memory for the data associated with those words. They also specify the run-time action of their children. The child word usually will put either the address of the data item or the item itself on the stack. For integer data, we use three defining words: CONSTANT, VALUE, and VARIABLE. Here is how each of these creates a new word in the dictionary:
| Example |
When word is created |
When word is used |
5 CONSTANT xxx |
Create a constant xxx initialized to 5 |
xxx puts 5 on the stack. A constant cannot be changed once it is defined. |
5 VALUE yyy |
Create a value yyy initialized to 5 |
yyy puts 5 on the stack but 7 TO yyy will change the stored value to 7. |
VARIABLE zzz |
Create a variable zzz (some implementations initialize to 0) |
zzz puts the address of the storage on the stack. Words @ (fetch) and ! (store) can access and store data. |