' STATIC Variables — persistence across calls, counter, accumulator
#COMPILE EXE
#DIM ALL

GLOBAL g_totalCalls AS LONG

FUNCTION Counter() AS LONG
    STATIC count AS LONG
    count = count + 1
    FUNCTION = count
END FUNCTION

FUNCTION Accumulator(n AS LONG) AS LONG
    STATIC sum AS LONG
    sum = sum + n
    FUNCTION = sum
END FUNCTION

FUNCTION RunningAverage(n AS LONG) AS DOUBLE
    STATIC total AS DOUBLE
    STATIC count AS LONG
    total = total + n
    count = count + 1
    FUNCTION = total / count
END FUNCTION

SUB ShowStats()
    STATIC minVal AS LONG
    STATIC maxVal AS LONG
    STATIC initialized AS LONG
    LOCAL current AS LONG
    current = Counter()
    IF initialized = 0 THEN
        minVal = current
        maxVal = current
        initialized = 1
    END IF
    IF current < minVal THEN minVal = current
    IF current > maxVal THEN maxVal = current
    PRINT "Call #" & STR$(current) & ": min=" & STR$(minVal) & " max=" & STR$(maxVal) & " range=" & STR$(maxVal - minVal)
END SUB

SUB FibonacciGenerator(BYREF val AS QUAD)
    STATIC a AS QUAD
    STATIC b AS QUAD
    STATIC started AS LONG
    IF started = 0 THEN
        a = 0
        b = 1
        started = 1
    END IF
    val = a
    LOCAL c AS QUAD
    c = a + b
    a = b
    b = c
END SUB

SUB PersistentDemo()
    LOCAL i AS LONG
    LOCAL val AS QUAD
    PRINT "=== STATIC PERSISTENCE DEMO ==="
    PRINT "Counter (persists across calls):"
    FOR i = 1 TO 5
        PRINT "  Counter() = " & STR$(Counter())
    NEXT i
    PRINT
    PRINT "Accumulator (persistent sum):"
    FOR i = 1 TO 10
        PRINT "  Accum(" & STR$(i) & ") = " & STR$(Accumulator(i))
    NEXT i
    PRINT
    PRINT "Running Average:"
    FOR i = 1 TO 10
        PRINT "  Avg(" & STR$(i) & ") = " & FORMAT$(RunningAverage(i), "##.##")
    NEXT i
    PRINT
    PRINT "ShowStats (min/max tracking):"
    FOR i = 1 TO 8
        CALL ShowStats()
    NEXT i
    PRINT
    PRINT "Fibonacci Generator (stateful):"
    FOR i = 1 TO 15
        CALL FibonacciGenerator(val)
        PRINT STR$(val)
        IF i MOD 5 = 0 THEN PRINT
    NEXT i
    PRINT
    PRINT
END SUB

SUB GlobalVsStatic()
    LOCAL i AS LONG
    PRINT "=== GLOBAL vs STATIC ==="
    PRINT "Calling functions multiple times..."
    FOR i = 1 TO 5
        g_totalCalls = g_totalCalls + 1
        Counter()
        PRINT "  Iteration " & STR$(i) & ": GLOBAL=" & STR$(g_totalCalls) & " STATIC(inside)=" & STR$(Counter() - 1)
    NEXT i
    PRINT
    PRINT "GLOBAL variables are visible everywhere."
    PRINT "STATIC variables are local but persist between calls."
    PRINT
END SUB

SUB RecursiveStaticCounter(n AS LONG)
    STATIC depth AS LONG
    LOCAL i AS LONG
    depth = depth + 1
    PRINT "Enter depth " & STR$(depth) & " (n=" & STR$(n) & ")"
    IF n > 1 THEN
        CALL RecursiveStaticCounter(n - 1)
    END IF
    depth = depth - 1
    PRINT "Exit depth " & STR$(depth + 1)
END SUB

SUB StateMachineDemo()
    STATIC state AS LONG
    LOCAL nextState AS LONG
    state = state + 1
    IF state > 5 THEN state = 1
    PRINT "=== STATE MACHINE (static state) ==="
    PRINT "State = " & STR$(state)
    SELECT CASE state
        CASE 1
            PRINT "  Initializing..."
        CASE 2
            PRINT "  Processing data..."
        CASE 3
            PRINT "  Validating results..."
        CASE 4
            PRINT "  Writing output..."
        CASE 5
            PRINT "  Completing..."
    END SELECT
    PRINT
END SUB

FUNCTION PBMAIN() AS LONG
    LOCAL i AS LONG
    PRINT "=============================="
    PRINT " STATIC Variable Persistence"
    PRINT "=============================="
    PRINT
    CALL PersistentDemo()
    CALL GlobalVsStatic()
    PRINT "Recursive counter with static depth:"
    CALL RecursiveStaticCounter(5)
    PRINT
    FOR i = 1 TO 7
        CALL StateMachineDemo()
    NEXT i
    PRINT "Static persistence demos complete."
    FUNCTION = 0
END FUNCTION
