Bad Results with Recursion of STATIC Procedure in QB.EXE (40553)






This article was previously published under Q40553

SYMPTOMS

The use of the STATIC clause in recursive functions or SUBprograms should be avoided. Using STATIC may cause you to overwrite values from a previous CALL. For example, recursively CALLing a STATIC SUBroutine and decrementing the passed parameter actually will change the value of the parameter when the procedure returns at the END SUB statement. However, the QuickBasic editor incorrectly allows the recursive use of a STATIC subroutine or function without any side effects of changing the passed parameters. This problem can lead to subtle programming errors because the incorrect results don't become apparent until compile time.

STATUS

Microsoft has confirmed this to be a bug in QuickBasic Versions 4.00, 4.00b, and 4.50 and in the QB.EXE program that is shipped with the Microsoft Basic Compiler Versions 6.00 and 6.00b for MS-DOS and MS OS/2. We are researching this problem and will post new information here in the Microsoft Knowledge Base as it becomes available.

MORE INFORMATION

More information on recursion in Version 4.00 or 4.00b can be found on Pages 81-82 of "Microsoft QuickBasic 4.0: Programming in Basic: Selected Topics." For QuickBasic Version 4.50, recursion is documented on Pages 71-72 of "Microsoft QuickBasic: Programming in Basic."

The following is a code example:
DECLARE SUB anysub (Param#)
CLS
 Param# = 10#
 CALL anysub(Param#)
END

SUB anysub (Param#) STATIC
  IF Param# > 1 THEN
     CALL anysub(Param# - 1)
  END IF
  PRINT Param#;
END SUB
				
You will get the following results when this code sample is run in the QB.EXE editor:
1  2  3  4  5  5  6  7  8  9  10
				
You will get the following results when it is run as an executable .EXE program:
1  1  1  1  1  1  1  1  1  1  10
				

Modification Type: Minor Last Reviewed: 1/9/2003
Keywords: KB40553