Forth DISCUSSION

Forth VARIABLE, VALUE, CONSTANT or locals: which should I use for data in a word?

Started by pramanandsah Forth VALUE and TOVARIABLECONSTANTlocal variablesfetch and store
5 replies 248 views 6 participants
Latest activity · 30 Sep 2026

Forth VARIABLE, VALUE, CONSTANT or locals: which should I use for data in a word?

pramanandsah Forth Forum
#1

My Forth code is full of @ and ! and I keep forgetting one, for example writing LIMIT 1 + when LIMIT is a variable and getting an address plus one. I have now seen VALUE with TO, and Gforth also supports local variables inside a definition.

What are the practical differences between VARIABLE, VALUE, CONSTANT and locals, and when is each the right choice? I would also like to understand how TO manages to store into a value when the value's name normally returns its contents.

Community replies 5

Re: Forth VARIABLE, VALUE, CONSTANT or locals: which should I use for data in a word?

#2

The three global kinds differ in what executing the name does. A VARIABLE pushes its address; you read with @ and write with !: after VARIABLE LIMIT and 100 LIMIT !, LIMIT @ . prints 100. A CONSTANT pushes its number and cannot be changed: 1000 CONSTANT MAX-RPM. A VALUE also pushes its number, but can be updated: 100 VALUE LIMIT, then LIMIT . prints 100 and 250 TO LIMIT changes it.

So LIMIT 1 + is correct for a value or a constant and wrong for a variable, where it must be LIMIT @ 1 +.

Re: Forth VARIABLE, VALUE, CONSTANT or locals: which should I use for data in a word?

#3

A reasonable rule: use CONSTANT for anything fixed at compile time, such as register addresses and bit masks; VALUE for settings that are read in many places and written in few, such as a calibration factor or a baud-rate divisor; and VARIABLE when you need the address itself. You need the address to pass it to another word, to use +! (1 COUNTER +! increments in place), or when the storage is a buffer or a multi-cell structure rather than a single number.

VALUE and TO belong to the core extension word set, so they are present in most but not all small systems.

Re: Forth VARIABLE, VALUE, CONSTANT or locals: which should I use for data in a word?

#4

TO works by looking ahead in the input. It is a parsing word: it reads the next name from the source, finds where that value's data is stored, and stores the top of the stack there. Inside a colon definition it does the lookup at compile time and compiles the address together with a store, so there is no search at run time.

A simple model of the pair is : VALUE CREATE , DOES> @ ; with TO doing the equivalent of ' name >BODY !. Real systems implement it more efficiently, and TO only accepts names defined by VALUE or as locals.

Re: Forth VARIABLE, VALUE, CONSTANT or locals: which should I use for data in a word?

#5

Locals exist only during one execution of one definition and are the tool for taming stack juggling inside a word. In Forth-2012 syntax: : HYP2 ( a b -- n ) {: a b :} a a * b b * + ;, and 3 4 HYP2 . prints 25. The names are initialised from the stack in the order written, so the last name gets the top item. Locals behave like values: using the name gives the number, and TO assigns. Older Gforth code uses the form { a b } for the same thing.

Because each call has its own copies, locals are safe in recursive words, which globals are not.

Re: Forth VARIABLE, VALUE, CONSTANT or locals: which should I use for data in a word?

#6

A caution about the globals, whichever kind you choose: they are shared by everything. A word that keeps its working data in a VARIABLE or VALUE is not re-entrant, so it will misbehave if it calls itself or is used from both an interrupt handler and the main loop. Multitasking Forths provide per-task storage, often called user variables, for that reason.

Traditional style prefers passing data on the stack and keeping definitions short enough that two or three stack items suffice. If a word needs more than about three items juggled with ROT and PICK, that is the signal to factor it into smaller words or to use locals, not to add more globals.

TEP COMMUNITY