OPTION EXPLICIT

Directive

Syntax

Makes declarations compulsory: every name the program writes to must be declared first, or the program does not compile.

OPTION EXPLICIT

Good to know

  • It is off by default. It applies to the whole program, included files too, wherever the line stands.
  • It stands alone on its line. Only a ' comment may follow it: OPTION EXPLICIT // note and OPTION EXPLICIT ON are refused.
  • Names that must be declared: the target of an assignment, a FOR counter, the target of INC, DEC and READ. The error reads OPTION EXPLICIT requires a declaration for variable: score.
  • Declarations: GLOBAL, LOCAL inside a FUNCTION, AS, a PureBasic suffix (speed.f = 1.5), DIM, NewList, NewMap, a FUNCTION parameter, a CATCH variable.
  • It checks the names you write, not the names you read. total = totl + 1 still compiles, and stops the program when the line runs: totl is not defined.

Example

OPTION EXPLICITGLOBAL score = 0GLOBAL i = 0lives AS integer = 3DIM grid(2)FUNCTION Hit(damage)  LOCAL rest = lives - damage  lives = restENDFUNCTIONHit(1)FOR i = 0 TO 2  grid(i) = i * 10  score = score + grid(i)NEXTConsole(Str(lives) + " " + Str(score))   ' 2 30
In the manual : Variables and types