3

Reading the Gforth manual, a value can be changed using the word TO, so how is it different than a variable?

https://gforth.org/manual/Values.html

2 Answers 2

5

VALUE takes an initial value, and the created word puts the value directly on the stack like CONSTANT. The value can still be changed using TO. Word definitions in many Forths using VALUE's will be smaller, because they just need to reference the created word and not !.

5 VALUE TERRYS
TERRYS . 5 ok

VARIABLE just reserves space for the value, uninitialised, and the created word puts the address of the variable on the stack instead.

VARIABLE TERRYS
5 TERRYS !
TERRYS @ . 5 ok

VARIABLE is useful when you want to take the address of the variable, and VALUE is useful when you don't need to.

If you want to initialise the variable, and be able to take the address, it is actually easier to just use CREATE and ,, like so:

CREATE TERRYS 5 ,
TERRYS @ . 5 ok
Sign up to request clarification or add additional context in comments.

Comments

2

if you define a word as 5 value A

when you type A

you get 5 put on the stack

when you type variable A

when you type A

you get an address put on the stack

to get the value inside, you use @

to write to it, you use !

value creates a word that puts a value on the stack

variable creates a word that puts an address on the stack

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.