STATIC Declaration

Syntax:
STATIC name AS type
STATIC name() AS type [subscripts]

Description

The STATIC statement declares variables that are local in scope to the current procedure but retain their values between calls. Unlike LOCAL variables, which are created fresh on each procedure entry, STATIC variables persist in memory for the lifetime of the program.

This is useful for counters that track how many times a function has been called, cached values that are expensive to recompute, or state that must survive across procedure invocations without using global variables.

Static variables are initialized to zero (numeric types) or empty string on first use. They are scoped to the procedure in which they are declared and are not visible to other procedures.

Parameters

ParameterDescription
nameThe identifier name of the variable. Must be unique within the procedure scope.
AS typeThe data type. Supported types include all standard numeric types, STRING, and user-defined TYPEs. See LOCAL documentation for the full type list.
subscriptsOptional. Specifies array bounds. Same syntax as LOCAL arrays.

Example

FUNCTION NextId() AS LONG
    STATIC counter AS LONG
    counter = counter + 1
    FUNCTION = counter
END FUNCTION

SUB LogMessage(BYVAL msg AS STRING)
    STATIC callCount AS LONG
    STATIC lastMsg AS STRING

    callCount = callCount + 1
    PRINT "Call #"; callCount; ": "; msg

    IF LEN(lastMsg) > 0 THEN
        PRINT "  (Previous: "; lastMsg; ")"
    END IF

    lastMsg = msg
END SUB

FUNCTION PBMAIN() AS LONG
    PRINT "First ID:  "; NextId()   ' 1
    PRINT "Second ID: "; NextId()   ' 2
    PRINT "Third ID:  "; NextId()   ' 3

    LogMessage "Alpha"
    LogMessage "Beta"
    LogMessage "Gamma"

    FUNCTION = 0
END FUNCTION

Generated Assembly

Data Section Placement

; STATIC variables are placed in the .data section of the PE binary,
; but with unique names scoped to the function to avoid collisions:
section .data
    __pb_NextId_counter dq 0           ; initialized to 0
    __pb_LogMessage_callCount dq 0
    __pb_LogMessage_lastMsg db 256 dup(0)  ; string buffer

Accessing STATIC Variables

; Read: x = counter
mov    rax, [__pb_NextId_counter]     ; direct memory access (not stack)

; Write: counter = counter + 1
mov    rax, [__pb_NextId_counter]
add    rax, 1
mov    [__pb_NextId_counter], rax     ; store back to static memory

; String assignment: lastMsg = msg
lea    rdi, [__pb_LogMessage_lastMsg]
lea    rsi, [rbp - msg_offset]
mov    rcx, 256
call   [__imp_memcpy]                 ; copy string to static buffer

Notes