What are STATIC variables and why do I care?
STATIC variables are similar to LOCALs in that they do not
"up-scope" or "down-scope." STATICs, however, keep their value
throughout the duration of the program, even when they are not
"visible." For example:
// STATIC1.PRG
FUNCTION caller
LOCAL i
? "BEGIN"
FOR i := 1 TO 100
? "Loop number ", i
callee()
NEXT
? "END"
RETURN NIL
FUNCTION callee
LOCAL l := 0
STATIC s := 0
l++
s++
? l, s
RETURN NIL
The output will look like this:
BEGIN
Loop number 1
1 1
Loop number 2
1 2
Loop number 3
1 3
Loop number 4
1 4
... ...
Loop number 100
1 100
END
Notice that each iteration of the loop increments the value of (s)
from the previous iteration, while the value of (l) starts all over
again from scratch.
The most common use of STATIC variables is to maintain values that
are useful to a group of functions, or to the whole application,
without resorting to global PRIVATE or PUBLIC variables. (See next
Question.)
STATIC variables carry the same features as LOCALs with regard to
symbol table entries and macro expansion as noted above.
STATICs work just like static variables in C and C++.