LOCAL statement   

Purpose

Declare local variables inside a Sub or Function.  Local variables retain their values only until the end of the Sub or Function.

Syntax

LOCAL variable[()] [AS type] [, variable[()]] [...]

LOCAL variable[()] [, variable[()]] [, ...] AS type

Remarks

The LOCAL statement is valid only inside a Sub or Function.  Local variables lose their values when the Sub or Function 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 Sub or Function 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, 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