Wiki source for KeyPgStatic


Show raw source

{{fbdoc item="title" value="STATIC"}}----
Defines variables, objects and arrays having static storage

{{fbdoc item="syntax"}}##
**Static** //symbol1// [ (//array-dimensions//) ] [[KeyPgAs|As]] [[DataType|DataType]] [ = //expression//] [, //symbol2// [ (//array-dimensions//) ] [[KeyPgAs|As]] [[DataType|DataType]] [ = //expression//], ...]
//or//
**Static** [[KeyPgAs|As]] [[DataType|DataType]] //symbol1// [ (//array-dimensions//) ] [ = //expression//] [, //symbol2// [ (//array-dimensions//) ] [ = //expression//], ...]

//or//

[[KeyPgSub|Sub]]|[[KeyPgFunction|Function]] //procedurename// ( //parameters// ) [[ [[KeyPgByrefFunction|byref]] ] [[KeyPgAs|As]] [[DataType|DataType]]] **Static**
...
[[KeyPgEndblock|End]] [[KeyPgSub|Sub]]|[[KeyPgFunction|Function]]
##
{{fbdoc item="param"}}
##//symbol//##
variable or array symbol name.
##//array-dimensions//##
##//lower-bound// [[KeyPgTo|To]] //upper-bound// [, ...]##
//or//
##[[KeyPgAny|Any]] [, [[KeyPgAny|Any]]...]##
//or empty.//
##//expression//##
An constant expression, or an array of constant expressions

{{fbdoc item="desc"}}
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 at procedure definition level (forbidden at declaration line level), ##**Static**## specifies [[ProPgStorageClasses|static storage]] for all local variables, objects and arrays, except temporary types and internal variables (objects not explicitly declared).

At module-level variable declaration only, the modifier ##[[KeyPgShared|Shared]]## may be used with the keyword ##**Static**## to make module-level static variables visible inside procedures.

When used with in a user-defined type, ##**Static**## creates [[KeyPgStaticMember|static member procedures or variables]].

{{fbdoc item="ex"}}
{{fbdoc item="filename" value="examples/manual/variable/static.bas"}}%%(freebasic)
sub f
'' times called is initially 0
static timesCalled as integer = 0
timesCalled += 1
Print "Number of times called: " & timesCalled
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
%%

{{fbdoc item="lang"}}
- Variables cannot be initialised in the //[[CompilerOptlang|-lang qb]]// dialect.

{{fbdoc item="diff"}}
- ""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**##.

{{fbdoc item="see"}}
- ##[[KeyPgStaticMember|Static (Member)]]##
- ##[[KeyPgDim|Dim]]##, ##[[KeyPgRedim|Redim]]##
- ##[[KeyPgShared|Shared]]##
- ##[[KeyPgByrefVariables|Byref (variables)]]##
- ##[[KeyPgSub|Sub (Module)]]##, ##[[KeyPgFunction|Function (Module)]]##
- ##[[KeyPgMemberSub|Sub (Member)]]##, ##[[KeyPgMemberFunction|Function (Member)]]##
- ##[[KeyPgOptionstatic|Option Static]]##
- ##[[ProPgStorageClasses|Storage Classes]]##
- [[ProPgProcedurePointers|Pointers to Procedures]]

{{fbdoc item="back" value="CatPgProcedures|Procedures"}}{{fbdoc item="back" value="CatPgVariables|Variable Declarations"}}
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki



sf.net phatcode