GLBasic User Manual

Main sections


LOCAL var#$, var#$[]

LOCAL var#=value#, var#=value(), ...

GLOBAL a#$ = LOCAL b#$
LOCAL a#$ = GLOBAL b#$

The LOCAL keyword indicates that a variable will have a local scope. This means that the variable is only available within the particular section of code (the main program/subroutine/function) that it's defined in. If a variable is declared LOCAL within the main program it is only available to the main program and isn't available to any subroutines/functions.

The memory used by a LOCAL variable is released at the end of the section of code (the main program/subroutine/function) that it's declared in.

If you use the keyword GLOBAL in front of a variable name 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.

LOCAL text$

GOSUB show
PRINT text$, 100, 150

// ------------------------------------------------------------- //
// -=# SHOW #=-
// ------------------------------------------------------------- //
SUB show:
LOCAL text$

// Creating and defining LOCAL arrays:
LOCAL array[]; DIM array[5]


PRINT text$, 100, 50
// Is equal to
// PRINT LOCAL text$, 100, 50
PRINT GLOBAL text$, 100, 100


See also...