Constants and variables
    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 ]

Forth also allows you to define your own constant and variables using the words 

CONSTANT

VARIABLE

When a constant is called up, this causes its value to be pushed onto the stack. However when a variable is called up this causes its address to be pushed onto the stack. The Forth words  !  and  @  are used to modify the contents of the variable.

A constant is defined by using the form:

value  CONSTANT name

and any reference the the name will cause the value to be put on the stack.

A variable is defined by using the form:

value  VARIABLE  name

and any reference to the name will result in the address of that variable to be put on the stack for further manipulation using  !  and  @. It is essential that you realize the difference between the constant and the address of a variable. Now for some examples:

64 CONSTANT  R  <CR>
1000 CONSTANT Q  <CR>
256 VARIABLE X  <CR>
0 VARIABLE Y  <CR>

R Q + .

 

will print the values of R + Qi.e. 1064

X .

 

will print the address of X not its value

X @ .

 

will print the value of X i.e. 256

R Y !

 

will store the value of R in the variable Y

Y X !

 

will store the address of Y in the variable X

4 X !

 

will store the value 4 in the variable X

BASIC command

Forth equivalent

LET X = Y

Y @ X !

LET X = R

R X !

LET X = 4

4 X !

LET X = X + 5

5 X +!

[ 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.
Hosted by www.Geocities.ws

1