<< Home | About Forth | About TurboForth | Download | Language Reference | Resources | Tutorials | YouTube >>


6 Values

Values are very similar to variables. However, you read and write them in a different way to variables. Some Forth systems do not support Values. TurboForth does.

Values in Forth are global; they can be accessed anywhere. They must be declared/defined before they can be used. Values should be declared outside of a colon-definition.

6.1 Declaring Values with VALUE

Values should be declared outside of a colon definition, like this:

    <inital value> VALUE Name

For example:

100 VALUE Hundred 

Here, the word VALUE makes a word in the Forth dictionary, called Hundred: Our value is called Hundred.

6.2 Writing Values to Values

It is important to understand that values, once created, as shown above, become words in the dictionary, just like other words. Values, like variables, can be executed in Forth. When a value is executed, it pushes its value. We write to a Value using the word TO, like this:

    123 TO MyVal

The above stores the value 123 into the Value MyVal. Note that the value comes first, then the word TO, then the name of the Value.

The word +TO can also be used to add a value to the value already stored in the Value (confusing, eh?) - like this:

    99 +TO MyVal

Here, 99 is added to whatever is stored in MyVal. Of course, this value can be negative:

    -1 +TO MyVal

Will reduce the value of MyVal by 1.

6.3 Reading Values from Values

Reading from values is also very simple. Simply name the word. Simply naming the value is enough to cause it to push its value to the stack:

MyVal .

Here, MyVal pushes its value to the stack, and . (dot) displays it.

<-- Back to Tutorials


<< Home | About Forth | About TurboForth | Download | Language Reference | Resources | Tutorials | YouTube >>