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 aboveDO.GLOBALis the fix, so declare withGLOBALevery value a function shares. - Give a single
GLOBALa value.GLOBAL hitsalone starts empty, not at 0, andhits + 1then givesNaN. The list formGLOBAL a, b$starts at 0 and"". GLOBALinside a FUNCTION creates the variable for the whole program: it can be read after the call.- Under
OPTION EXPLICIT,GLOBALcounts as a declaration. GLOBAL DIMdoes not work reliably: write a plainDIMat 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 [] 0In the manual : Variables and types