LOCAL statement

Purpose | Declare local variables inside a Sub, Function, Method, or Property. Local variables retain their values only until the end of the procedure.
Syntax | LOCAL variable[()] [AS type] [, variable[()]] [...] LOCAL variable[()] [, variable[()]] [, ...] AS type

Remarks

The LOCAL statement is valid only inside a Sub, Function, Method, or Property. Local variables lose their values when the procedure ends. Storage space for local variables is allocated on the stack, and each local variable is initialized to zero (or, for string variables, an empty string) each time the enclosing procedure is called.

To declare an array as a local variable, use an empty set of parentheses in the variable list: You can then use the DIM statement to dimension the array.

LOCAL MyArray%()

LOCAL StringArray() AS STRING

The LOCAL statement may, optionally, accept a list of variables, all of which are defined by the type descriptor keyword that follows them. For example:

LOCAL aaa, bbb, ccc AS INTEGER

LOCAL vptr, aptr() AS LONG PTR

Restrictions | DEFtype has no effect on variables defined by a LOCAL statement.
See also | DIM, GLOBAL, INSTANCE, STATIC, THREADED

Example

Test% = 100

ShowText "Before: " + STR$(Test%)

CALL Locals

ShowText "After: " + STR$(Test%)

SUB Locals

LOCAL Test%

Test% = 0

ShowText "In SUB: " + STR$(Test%)

END SUB

Result | Before: 100 In SUB: 0 After: 100