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.
Define name.
name execution: ( – w )
Like constant
, but defines a constant for an address
(this only makes a difference in the cross-compiler).
Define name.
name execution: ( – w1 w2 )
Define name.
name execution: ( – r )
Well, often it can be – but
not in a Standard, portable way. It’s safer to use a Value
(read
on).