6.11.3 Constants

Constant allows you to declare a fixed value and refer to it by name. For example:

12 Constant INCHES-PER-FOOT \ is integer appropriate
2.54e fconstant CM-PER-INCH

A Variable can be both read and written, so its run-time behaviour is to supply an address through which its current value can be manipulated. In contrast, the value of a Constant cannot be changed once it has been declared17 so it’s not necessary to supply the address – it is more efficient to return the value of the constant directly. That’s exactly what happens; the run-time effect of a constant is to put its value on the top of the stack (You can find one way of implementing Constant in User-defined Defining Words).

Forth also provides 2Constant and fconstant for defining double and floating-point constants, respectively.

Constant ( w "name" –  ) core “Constant”

Define name.
name execution: ( – w )

AConstant ( addr "name" –  ) gforth-0.2 “AConstant”

Like constant, but defines a constant for an address (this only makes a difference in the cross-compiler).

2Constant ( w1 w2 "name" –  ) double “two-constant”

Define name.
name execution: ( – w1 w2 )

fconstant ( r "name" –  ) floating “f-constant”

Define name.
name execution: ( – r )


Footnotes

(17)

Well, often it can be – but not in a Standard, portable way. It’s safer to use a Value (read on).