Microsoft KB Archive/43930

From BetaArchive Wiki
Knowledge Base

Article ID: 43930

Article Last Modified on 8/16/2005


  • 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


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 concerning recursive procedures can be found on Pages 79-83 of the "Microsoft QuickBasic 4.0: Programming in Basic: Selected Topics" manual for Versions 4.00 and 4.00b, and on Pages 69-72 of the "Microsoft QuickBasic 4.5: Programming in Basic" manual for Version 4.50.

Code Example

The following program can be found in either the "Microsoft QuickBasic 4.0: Basic Language Reference" manual on Pages 62-63, or on Pages 50-51 of the ring-bound "Microsoft QuickBasic 4.5: Basic Language Reference" manual for Version 4.50:

DECLARE FUNCTION Reverse$ (StringVar$)
LINE INPUT "Enter string to reverse: ", X$
PRINT Reverse$(X$)

FUNCTION Reverse$ (S$)
  C$ = MID$(S$, 1, 1)
  IF C$ = "" THEN
    Reverse$ = ""
    Reverse$ = Reverse$(MID$(S$, 2)) + C$

Additional query words: QuickBas BasicCom

Keywords: KB43930