Hello Bert
On Saturday, 21 March 1998 21:53:00, you wrote to All:
BB> I read somewhere that procedures should be STATIC, as in:
BB> SUB DoIt (s$) STATIC
BB> FUNCTION DoItAgain (s$) STATIC
BB> Anybody know what this does and why it matters?
Normally all local variables within procedures (subs and functions) are
created on the stack when entering the procedure and erased when
leaving. So they are not only local but temporary as well. However,
local variables can be made permanent (for the duration of the program's
execution) by declaring them as static, for instance:
SUB DoIt()
DIM x AS INTEGER
STATIC y AS INTEGER
....
x is a temporary local variable, and y is a permanent local variable.
A feature of static variables is that they retain their value when the
procedure ends. An example:
SUB PrintIt()
DIM x AS INTEGER
STATIC y AS INTEGER
PRINT "x ="; x
IF x = 0 THEN x = 2
PRINT "y ="; y
IF y = 0 THEN y = 2
END SUB
When PrintIt is executed the first time it will print
x = 0
y = 0
but the second time it prints
x = 0
y = 2
A drawback of static variables is that they permanently occupy memory,
even before the procedure is called. That's not such a big problem with
simple integer variables, but it can be with arrays and strings.
What happens when you make a whole procedure static is that all local
variables of that procedure are static. Normally that will not be a good
idea because static variables permanently occupy memory, and they can
have an unknown value on entrance.
Friendly greeting you,
Hans Lunsing, Fido : 2:500/104.6955
Internet : jlunsing@doge.nl
--- Terminate 5.00/Pro
---------------
* Origin: HCC DOSgg SW Boss West 1, ++31793317774 (2:500/104.6955)
|