Main sections
GLOBAL
GLOBAL var#$, var#$[]
GLOBAL var#=value#, var#=value(), ...
GLOBAL a#$ = LOCAL b#$
LOCAL a#$ = GLOBAL b#$
The GLOBAL keyword indicates that a variable will have a global scope. This means that the variable is available within any of your code, not just the section (the main program/subroutine/function) that it's defined in.
If you use the keyword GLOBAL in front of a LOCAL variable name (within a function/subroutine etc) it will reference the global version of that variable - even if a LOCAL variable of the same name exists.
Without specifying LOCAL or GLOBAL in front of a variable name, GLBasic tries to use a LOCAL variable with that name first. If there is no LOCAL variable with that name defined then the GLOBAL one is used.
You define a variable as GLOBAL explicitly with:
GLOBAL var#$
If you do not explicitly create a GLOBAL variable before assigning it a value you will get an "implicitly created GLOBAL" warning.
If you assign a result from a command like MOUSESTATE or GETSCREENSIZE to a variable that has not been explicitly created you will get a "variable probably unassigned" warning from the compiler.
Sample:
LOCAL text$
text$="LOCAL_MAIN"
GLOBAL text$="GLOBAL"
GOSUB show
PRINT text$, 100, 150
SHOWSCREEN
MOUSEWAIT
END
// ------------------------------------------------------------- //
// -=# SHOW #=-
// ------------------------------------------------------------- //
SUB show:
LOCAL text$
// Creating and defining LOCAL arrays:
LOCAL array[]; DIM array[5]
text$="LOCAL_SUB"
PRINT text$, 100, 50
// Is equal to
// PRINT LOCAL text$, 100, 50
PRINT GLOBAL text$, 100, 100
ENDSUB // SHOW
Output:
LOCAL_SUB
LOCAL_MAIN