Syntax

Declares a variable shared by the whole program, every FUNCTION included. It works anywhere, even inside a FUNCTION.

GLOBAL <name> [= <value>]
GLOBAL <name> AS <type> [= <value>]
GLOBAL <name>, <name>[, <name>…]
GLOBAL NewList <name>.<type>()
GLOBAL NewMap <name>.<type>()

Parts

<name>The variable's name. A $ name in the list form starts at "", any other at 0.
<value>Optional starting value: any expression.
<type>A number, string or record type for AS; the type of the elements for NewList and NewMap.

Good to know

  • In a program with a DO … LOOP, a FUNCTION does not see a plain variable, a typed variable, a list or a map created above DO. GLOBAL is the fix, so declare with GLOBAL every value a function shares.
  • Give a single GLOBAL a value. GLOBAL hits alone starts empty, not at 0, and hits + 1 then gives NaN. The list form GLOBAL a, b$ starts at 0 and "".
  • GLOBAL inside a FUNCTION creates the variable for the whole program: it can be read after the call.
  • Under OPTION EXPLICIT, GLOBAL counts as a declaration.
  • GLOBAL DIM does not work reliably: write a plain DIM at the top of the program.

Example

GLOBAL score = 0GLOBAL lives AS integer = 3GLOBAL name$, levelFUNCTION AddPoints(points)  score = score + pointsENDFUNCTIONAddPoints(10)AddPoints(5)Console(Str(score) + " " + Str(lives) + " [" + name$ + "] " + Str(level))   ' 15 3 [] 0
In the manual : Variables and types