QB / QB64 Discussion Forum      Other Subforums, Links and Downloads
 

 Return to Index  

Local variables in subprograms always re-initialize

July 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"
PRINT
WHILE k$ <> CHR$(27)

subprogram
SLEEP
PRINT "out of subprogram..."
PRINT
k$ = INKEY$

WEND

SUB subprogram

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
PRINT "(still in subprogram)"
PRINT
PRINT "this shows the values have just been increased - but won't be retained"

PRINT "lv1% ="; lv1%; "lv2% ="; lv2%; "lv5% ="; lv5%
PRINT
PRINT
END SUB


 
 Respond to this message   
Responses

 Copyright © 1999-2014 Network54. All rights reserved.   Terms of Use   Privacy Statement