' Scope Demo — LOCAL, GLOBAL, STATIC variable scope
#COMPILE EXE
#DIM ALL

GLOBAL g_counter AS LONG

FUNCTION CountCalls() AS LONG
    STATIC calls AS LONG
    LOCAL n AS LONG

    g_counter = g_counter + 1
    calls = calls + 1
    n = g_counter + calls

    PRINT "GLOBAL = " + STR$(g_counter) + " STATIC = " + STR$(calls) + " LOCAL = " + STR$(n)

    FUNCTION = calls
END FUNCTION

FUNCTION PBMAIN() AS LONG
    LOCAL i AS LONG
    LOCAL r AS LONG

    PRINT "Scope demonstration:"
    PRINT STRING$(40, 45)
    PRINT

    FOR i = 1 TO 4
        r = CountCalls()
        PRINT "Call " + STR$(i) + " returned " + STR$(r)
    NEXT i

    PRINT
    PRINT "Final GLOBAL g_counter = " + STR$(g_counter)

    FUNCTION = 0
END FUNCTION
