Declares a static variable in a function or file or declares a static function in a file.
STATIC var1, var2, ..., varN
varN |
- |
One or more variable names. |
sumval(x)
{
static sum = 0;
sum += x;
return(sum);
}
The function sumval returns the sum of the current input value and all previous values. The static variable sum behaves like a local variable except it is persistent from one call to sumval to another.
// sumval.spl
static sum = 0;
sumval(x)
{
sum += x;
return(sum);
}
currsum()
{
return(sum);
}
Static variables can be file based such that all functions within the same SPL file have access to the variable. In this case, sumval behaves identically to the previous example and currsum returns the current sum.
The static key word also specifies static functions. Like static variables, a static function is private to the SPL source file. Only functions defined in the same SPL file as the static function have access to the static function. For example:
// myfun.spl
static statfun()
{
return("myfun statfun");
}
myfun(x)
{
local y;
y = statfun();
return(y);
}
// statfun.spl
statfun()
{
return("statfun);
}
a = statfun();
b = myfun();
a == "statfun"
b == "myfun statfun"
The function statfun defined in statfun.spl is a standard global function. The function statfun defined in myfun.spl is hidden from all functions except those functions defined in myfun.spl. Within myfun.spl, the static definition of statfun overrides global definitions of the same name, if any.
Static variables are persistent from one call of a function to another. As shown in the examples, static variables can be local to a particular function or local to all functions within particular SPL file. A static variable behaves like a local variable in that variable is only accessible to the function that declares the static variable or if file based, to the functions declared within the same file.
Static variables are initialized when the SPL function is first loaded, whether automatically loaded or explicitly loaded. If no initialization value is given, the variable is initialized to 0.
A static function defined in an SPL file should come before any global or static function that uses it.
See LOCAL to define a variable that is local to an SPL function without persistence.
See GLOBAL or EXTERN to define a global variable in an SPL function.
See NONLOCAL to define a variable that can be private to one SPL function but accessible to another.