#CONSTANT

Directive

Syntax

Names a fixed value. The constant is read back with # in front of its name, anywhere in the program.

#CONSTANT <NAME> <value>
#CONSTANT <NAME> = <value>
#<NAME>

Parts

<NAME>The constant's name, case-insensitive, with no sigil. It must not be a catalogue command name or a built-in #AK_… constant.
<value>A number, a string, or an expression of other constants.

Good to know

  • Constants are set before the first line runs, so a constant can be read above its own line.
  • A name declared twice is refused: Duplicate BASIC constant: #LIVES. A command name is refused too (#CONSTANT LEFT 1): pick a name such as #DIR_LEFT.
  • A constant and a variable spelled exactly alike, case included, compile into a program that does not start. Give them different names.
  • A sigil on the name (#CONSTANT NAME$ "x") compiles, but the program does not start. A misspelled #SPEDE and a write #SPEED = 6 are found only when the line runs.
  • In a program with a DO … LOOP, a FUNCTION cannot read a constant: it stops with … is not defined. Copy it into a GLOBAL and read that.
  • PureBasic's #NAME = 5 is not a constant declaration outside an ENUMERATION. Write #CONSTANT NAME 5.

Example

Console(Str(#MAX_LIVES))   ' 3#CONSTANT MAX_LIVES 3#CONSTANT TITLE "Space Rocks"#CONSTANT GRAVITY = 9.81#CONSTANT DOUBLE_G #GRAVITY * 2Console(#title + " " + Str(#DOUBLE_G))   ' Space Rocks 19.62
In the manual : Constants and operators