Revision [15574]
This is an old revision of KeyPgStatic made by FxMwikki on 2011-12-20 03:33:32.
STATIC
Defines variables, objects and arrays having static storage
Syntax:
Static symbol [ ( [ subscripts ] ) ] [ KeyPgAs as DataType DataType ] [ , ... ]
or
{ KeyPgSub sub|KeyPgFunction function } proc_header Static
[ ... ]
KeyPgEndblock end { KeyPgSub sub|KeyPgFunction function }Parameters:
symbol
variable or array symbol name.
subscripts
a comma-separated list of subscript ranges.
proc_header
procedure header for a procedure definition.
Description:
Specifies ProPgStorageClasses static storage for variables, objects and arrays; they are allocated at program startup and deallocated upon exit. Objects are constructed once when they are defined, and destructed upon program exit.
When declaring static arrays, only ProPgLiterals numeric literals, KeyPgConst constants or KeyPgEnum enumerations may be used as subscript range values. Static variable-length arrays must be declared empty (no subscript range list) and resized using KeyPgRedim Redim before used.
In both iterative and recursive blocks, like looping CatPgControlFlow control flow statements or procedures, static variables, objects and arrays local to the block are guaranteed to occupy the same storage across all instantiations of the block. For example, procedures that call themselves - either directly or indirectly - share the same instances of their local static variables.
A static variable may only be initialised with a constant value: its starting value is set at the start of the program before any code is run, and so it cannot depend on any variables or functions in it.
When used with module-level and member procedure declarations, Static specifies ProPgStorageClasses static storage for all local variables, objects and arrays.
KeyPgStaticMember Static (Member) is also used in member procedure declarations to specify static member procedures.
Examples:
Sub f
'' static variables are initialized to 0 by default
Static i As Integer
i += 1
Print "Number of times called: " & i
End Sub
'' the static variable in f() retains its value between
'' multiple procedure calls.
f()
f()
'' static variables are initialized to 0 by default
Static i As Integer
i += 1
Print "Number of times called: " & i
End Sub
'' the static variable in f() retains its value between
'' multiple procedure calls.
f()
f()
Will output:
Number of times called: 1 Number of times called: 2
Differences from QB:
- QuickBASIC allows variables and arrays to be declared using the Static keyword within procedures and DEF FN routines only.
- Static forces local visibility of variables and arrays in QuickBASIC DEF FN routines. FreeBASIC supports neither DEF FN routines nor this usage of Static.
- Using KeyPgStaticMember Static to specify static member procedures is new to FreeBASIC.
See also:
- KeyPgStaticMember Static (Member)
- KeyPgDim Dim, KeyPgRedim Redim
- KeyPgSub Sub (Module), KeyPgFunction Function (Module)
- KeyPgMemberSub Sub (Member), KeyPgMemberFunction Function (Member)
- KeyPgOptionstatic Option Static
- ProPgStorageClasses Storage Classes
Back to Procedures
Back to Variable Declarations