Local variables in subprograms always re-initializeJuly 22 2012 at 8:46 AM
|AlGoreIthm (Login AlGoreIthm)|
Is there anyway to have local variables in a subprogram retain their values?
Other than using COMMON SHARE, I have no way of doing it
- this program demonstrates that the variables change values but re-initialize
to zero automatically when the subprogram is called again.
Maybe there's an obviously simple solution to this that I've overlooked ... ?
Challenge: find a way for local variables to retain their values, without using COMMON SHARED
- possibly, if the values are low enough, simple POKEs to free memory might do it...but what if the values were large or extra-large?
DECLARE SUB subprogram ()
COMMON SHARED counter%
PRINT " Hit the ESC key to end the program"
WHILE k$ <> CHR$(27)
PRINT "out of subprogram..."
k$ = INKEY$
counter% = counter% + 1
PRINT "This is pass #"; counter%; "and the local variables are set to:"
PRINT "lv1% = "; lv1%
PRINT "lv2% = "; lv2%
PRINT "lv5% = "; lv5%
lv1% = lv1% + 1
lv2% = lv2% + 2
lv5% = lv5% + 5
PRINT "(still in subprogram)"
PRINT "this shows the values have just been increased - but won't be retained"
PRINT "lv1% ="; lv1%; "lv2% ="; lv2%; "lv5% ="; lv5%
- STATIC - Clippy on Jul 22, 2012