STATIC: Difference between revisions
Jump to navigation
Jump to search
Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link
TheSnowDog (talk | contribs) m (QBasic capitalisation) Tag: visualeditor |
No edit summary |
||
(6 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
The | The [[STATIC]] keyword is used in declaration statements to control where variables are stored. | ||
{{PageSyntax}} | {{PageSyntax}} | ||
: | ;Variables:[[STATIC]] {{Parameter|variableName}}[()] [[[AS]] {{Parameter|dataType}}][, ...] | ||
;Subs & Functions:{[[SUB]]|[[FUNCTION]]} {{Parameter|procedureName}} [({{Parameter|parameterList}})] '''STATIC''' | |||
;Library function block:[[DECLARE LIBRARY|DECLARE STATIC LIBRARY]] | |||
:{[[SUB]]|[[FUNCTION]]} {{Parameter|procedureName}} [({{Parameter|parameterList}})] STATIC | |||
{{PageDescription}} | {{PageDescription}} | ||
* A STATIC list can be used in [[SUB]] and [[FUNCTION]] procedures to designate one or more variables to retain their values. | * A STATIC list can be used in [[SUB]] and [[FUNCTION]] procedures to designate one or more variables to retain their values. | ||
* Variables and arrays with static storage will retain their values in between procedure calls. The values may also be used recursively. | * Variables and arrays with static storage will retain their values in between procedure calls. The values may also be used recursively. | ||
:* {{Parameter|variableName}} may include a type suffix or use [[AS]] to specify a type other than the default [[SINGLE]] type. | :* {{Parameter|variableName}} may include a type suffix or use [[AS]] to specify a type other than the default [[SINGLE]] type. | ||
Line 19: | Line 17: | ||
{{PageExamples}} | |||
''Example 1: Finding the binary bit settings from a 32 bit [[LONG]] register return using recursion. | ''Example 1: Finding the binary bit settings from a 32 bit [[LONG]] register return using recursion. | ||
{{CodeStart}} | {{CodeStart}} | ||
{{Cl|INPUT}} "Enter a numerical value to see binary value: ", num& | {{Cl|INPUT}} "Enter a numerical value to see binary value: ", num& | ||
{{Cl|PRINT}} | {{Cl|PRINT}} BinStr$(num&) | ||
{{Cl|END}} | {{Cl|END}} | ||
{{Cl|FUNCTION}} | {{Cl|FUNCTION}} BinStr$ (n&) {{Cl|STATIC}} 'comment out STATIC to see what happens! | ||
{{Cl|DIM}} p%, s$ | {{Cl|DIM}} p%, s$ | ||
{{Cl|IF...THEN|IF}} 2 ^ p% > n& {{Cl|THEN}} | {{Cl|IF...THEN|IF}} 2 ^ p% > n& {{Cl|THEN}} | ||
Line 34: | Line 33: | ||
{{Cl|IF...THEN|IF}} n& > 2 ^ p% {{Cl|THEN}} | {{Cl|IF...THEN|IF}} n& > 2 ^ p% {{Cl|THEN}} | ||
p% = p% + 1 | p% = p% + 1 | ||
s$ = | s$ = BinStr$(n&) 'recursive call to itself | ||
{{Cl|ELSE}}: p% = 0 | {{Cl|ELSE}}: p% = 0 | ||
{{Cl|END IF}} | {{Cl|END IF}} | ||
{{Cl|END IF}} | {{Cl|END IF}} | ||
{{Cl|IF...THEN|IF}} s$ = "" {{Cl|THEN}} | {{Cl|IF...THEN|IF}} s$ = "" {{Cl|THEN}} BinStr$ = "0" {{Cl|ELSE}} BinStr$ = s$ | ||
{{Cl|END FUNCTION}} | {{Cl|END FUNCTION}} | ||
{{CodeEnd}} | {{CodeEnd}} | ||
: ''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!''' | : ''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!''' | ||
Line 55: | Line 54: | ||
{{Cl|STATIC}} resultCache() {{Cl|AS}} {{Cl|DOUBLE}} | {{Cl|STATIC}} resultCache() {{Cl|AS}} {{Cl|DOUBLE}} | ||
{{Cl|STATIC}} firstCall {{Cl|AS}} {{Cl|INTEGER}} | {{Cl|STATIC}} firstCall {{Cl|AS}} {{Cl|INTEGER}} | ||
' The lookup table is initially empty, so re-size it.. | ' The lookup table is initially empty, so re-size it.. | ||
{{Cl|IF...THEN|IF}} firstCall = 0 {{Cl|IF...THEN|THEN}} | {{Cl|IF...THEN|IF}} firstCall = 0 {{Cl|IF...THEN|THEN}} | ||
firstCall = -1 | firstCall = -1 | ||
{{Cl|REDIM}} resultCache(maxNToCache) {{Cl|AS}} {{Cl|DOUBLE}} | {{Cl|REDIM}} resultCache(maxNToCache) {{Cl|AS}} {{Cl|DOUBLE}} | ||
' ..and pre-calculate some factorials. | ' ..and pre-calculate some factorials. | ||
resultCache(0) = 1 | resultCache(0) = 1 | ||
Line 66: | Line 65: | ||
resultCache(2) = 2 | resultCache(2) = 2 | ||
{{Cl|IF...THEN|END IF}} | {{Cl|IF...THEN|END IF}} | ||
' See if we have the result cached. If so, we're done. | ' See if we have the result cached. If so, we're done. | ||
{{Cl|IF...THEN|IF}} n <= maxNToCache {{Cl|IF...THEN|THEN}} | {{Cl|IF...THEN|IF}} n <= maxNToCache {{Cl|IF...THEN|THEN}} | ||
Line 74: | Line 73: | ||
{{Cl|IF...THEN|END IF}} | {{Cl|IF...THEN|END IF}} | ||
{{Cl|IF...THEN|END IF}} | {{Cl|IF...THEN|END IF}} | ||
' If not, we use recursion to calculate the result, then cache it for later use: | ' If not, we use recursion to calculate the result, then cache it for later use: | ||
resultCache(n) = {{Cl|INT}}(n) * Factorial({{Cl|INT}}(n) - 1) | resultCache(n) = {{Cl|INT}}(n) * Factorial({{Cl|INT}}(n) - 1) | ||
Line 81: | Line 80: | ||
{{CodeEnd}} | {{CodeEnd}} | ||
{{OutputStart}} | {{OutputStart}} | ||
1 | 1 | ||
120 | 120 | ||
Line 91: | Line 90: | ||
* [[DIM]], [[REDIM]], [[COMMON]] | * [[DIM]], [[REDIM]], [[COMMON]] | ||
* [[SUB]], [[FUNCTION]] | * [[SUB]], [[FUNCTION]] | ||
* [[DECLARE LIBRARY]] | |||
* [[TYPE]], [[Arrays]] | * [[TYPE]], [[Arrays]] | ||
* [[$STATIC]], [[$DYNAMIC]] | * [[$STATIC]], [[$DYNAMIC]] | ||
* [[Data types]] | * [[Data types]] | ||
{{PageNavigation}} | {{PageNavigation}} |
Latest revision as of 21:20, 5 April 2023
The STATIC keyword is used in declaration statements to control where variables are stored.
Syntax
- Variables
- STATIC variableName[()] [[[AS]] dataType][, ...]
- Subs & Functions
- {SUB|FUNCTION} procedureName [(parameterList)] STATIC
- Library function block
- DECLARE STATIC LIBRARY
Description
- A STATIC list can be used in SUB and FUNCTION procedures to designate one or more variables to retain their values.
- Variables and arrays with static storage will retain their values in between procedure calls. The values may also be used recursively.
- STATIC can be used after the name of a SUB or FUNCTION in the procedure to force all variables to retain their values.
- Recursive procedures may be required to be STATIC to avoid a Stack Overflow! QB64 programs may just close!
- $STATIC defined program arrays cannot be re-sized or use _PRESERVE.
Examples
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 BinStr$(num&) END FUNCTION BinStr$ (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$ = BinStr$(n&) 'recursive call to itself ELSE: p% = 0 END IF END IF IF s$ = "" THEN BinStr$ = "0" ELSE BinStr$ = 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!
Example 2: 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