QB64.com

QB64 is a modern extended BASIC programming language that retains QBasic/QuickBASIC 4.5 compatibility and compiles native binaries for Windows, Linux, and macOS.

The STATIC keyword is used in declaration statements to control where variables are stored.

Syntax

STATIC variableName[()] [AS dataType][, …] STATIC [AS dataType] variableName[()][, …]

Syntax

{SUB FUNCTION} procedureName [(parameterList)] STATIC

Description

Example(s)

*Example 1: Finding the binary bit settings from a 32 bit LONG register return using recursion.


INPUT "Enter a numerical value to see binary value: ", num&
PRINT Bin$(num&)

END

FUNCTION Bin$ (n&) STATIC 'comment out STATIC to see what happens!
  DIM p%, s$
  IF 2 ^ p% > n& THEN
    p% = 0
  ELSE
    IF n& AND 2 ^ p% THEN s$ = "1" + s$ ELSE s$ = "0" + s$
    IF n& > 2 ^ p% THEN
      p% = p% + 1
      s$ = Bin$(n&) 'recursive call to itself
    ELSE: p% = 0
    END IF
  END IF
  IF s$ = "" THEN Bin$ = "0" ELSE Bin$ = s$
END FUNCTION 

Explanation: The FUNCTION above returns a STRING value representing the bits ON in an INTEGER value. The string can be printed to the screen to see what is happening in a port register. STATIC keeps the function from overloading the memory “Stack” and is normally REQUIRED when recursive calls are used in QBasic! QB64 procedures will close without warning or error!

Using a static array to cache factorials, speeding up repeated calculations:


PRINT Factorial(0)
PRINT Factorial(5)
PRINT Factorial(50)

FUNCTION Factorial# ( n AS DOUBLE )
    CONST maxNToCache = 50
    STATIC resultCache() AS DOUBLE
    STATIC firstCall AS INTEGER
    
    ' The lookup table is initially empty, so re-size it..
    IF firstCall = 0 THEN
        firstCall = -1
        REDIM resultCache(maxNToCache) AS DOUBLE
        
        ' ..and pre-calculate some factorials.
        resultCache(0) = 1
        resultCache(1) = 1
        resultCache(2) = 2
    END IF
    
    ' See if we have the result cached. If so, we're done.
    IF n <= maxNToCache THEN
        IF resultCache(n) <> 0 THEN
            Factorial = resultCache(n)
            EXIT FUNCTION
        END IF
    END IF
    
    ' If not, we use recursion to calculate the result, then cache it for later use:
    resultCache(n) = INT(n) * Factorial(INT(n) - 1)
    Factorial = resultCache(n)
END FUNCTION



 1
 120
 3.041409320171338D+64

See Also