Home > Language Reference > Introduction to the Language > Global and Local Variables

Global and Local Variables

A Global variable can be referred to from anywhere in the script. A Local variable is accessible only from the function/Sub in which it is declared.

As arrays and unsigned variables must be declared at the beginning of the script using Dim statement, arrays and unsigned can be only global.

A local variable must be defined before any statement within the procedure or function, by the use of keyword LOCAL. 

Such local variables exist only during execution of the Sub or Function.

Variable type is implicit depending what character is at the end ot its identifier: if character $ ends its identifier, it is a string local variable, if not, it is an integer. A local integer variable is initialized to 0 by default, a local string variable is initialized to an empty string by default ("").

32 local variables maximum can be defined in a Function or Sub.

Example:
Function my_function()
    Local myindex, myname$
    ...
    Endfunction

See also

Using Functions
Constants and Variables
Using Arrays
Expressions and Operators
Visibility of Variables
Script Structure