Microsoft KB Archive/43930

= Recursive Procedure Variables Not Saved When in COMMON =

Article ID: 43930

Article Last Modified on 8/16/2005

-

APPLIES TO


 * Microsoft QuickBasic 4.0
 * Microsoft QuickBASIC 4.0b
 * Microsoft QuickBasic 4.5 for MS-DOS
 * Microsoft BASIC Compiler 6.0
 * Microsoft BASIC Compiler 6.0b
 * Microsoft BASIC Professional Development System 7.0

-



This article was previously published under Q43930



SUMMARY
QuickBasic procedures can recursively CALL themselves. Any needed variables to be used in the recursive procedure should be passed as parameters, and all local variables (variables dimensioned only in the SUB itself) variables whose values need to be preserved through each recursive CALL should be declared as STATIC. STATIC declaration causes each variable's values to be retained on the stack for later use.

One important note is that all variables that are located in a COMMON or COMMON SHARED block and are used in a recursive procedure will not be saved through each recursive CALL.

This information applies to Microsoft QuickBasic Versions 4.00, 4.00b, and 4.50 for MS-DOS, Microsoft Basic Compiler Versions 6.00 and 6.00b for MS-DOS and MS OS/2, and Microsoft Basic PDS Version 7.00 for MS-DOS and MS OS/2.



MORE INFORMATION
More information concerning recursive procedures can be found on Pages 79-83 of the &quot;Microsoft QuickBasic 4.0: Programming in Basic: Selected Topics&quot; manual for Versions 4.00 and 4.00b, and on Pages 69-72 of the &quot;Microsoft QuickBasic 4.5: Programming in Basic&quot; manual for Version 4.50.

Code Example
The following program can be found in either the &quot;Microsoft QuickBasic 4.0: Basic Language Reference&quot; manual on Pages 62-63, or on Pages 50-51 of the ring-bound &quot;Microsoft QuickBasic 4.5: Basic Language Reference&quot; manual for Version 4.50: DECLARE FUNCTION Reverse$ (StringVar$) LINE INPUT &quot;Enter string to reverse: &quot;, X$ PRINT Reverse$(X$) END

FUNCTION Reverse$ (S$) C$ = MID$(S$, 1, 1) IF C$ = &quot;&quot; THEN Reverse$ = &quot;&quot; ELSE Reverse$ = Reverse$(MID$(S$, 2)) + C$ END IF END FUNCTION

Additional query words: QuickBas BasicCom

Keywords: KB43930

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.