"Subprogram Already in Use" with Recursive SUB CALL (31888)






This article was previously published under Q31888

SUMMARY

The message "Subprogram Already in Use" (error 36) is generated when trying to recursively call a STATIC subprogram in an interpreted or compiled program. This is because the STATIC argument must be removed from the SUB statement to make it a dynamic subprogram, which can then be called recursively.

Any attempt to call a dynamic subprogram in the interpreter will give a "Missing STATIC in SUB statement" message. The Macintosh QuickBASIC Interpreter does not support dynamic subprograms (i.e., recursive subprogram calls). Only compiled programs support recursive subprogram calls.

MORE INFORMATION

Both the compiler in QuickBASIC and the older Microsoft BASIC Compiler Version 1.00 for Apple Macintosh support recursive, dynamic SUBprogram CALLs.

The interpreter in QuickBASIC and the Microsoft BASIC Interpreter Versions 2.00, 2.10, and 3.00 for the Apple Macintosh do not support SUBprogram recursion, and require the STATIC argument on the SUB statement.

The program below contains a recursive (dynamic) subprogram which calls itself five times. If this program is executed in the Interpreter, the error will occur. As a compiled program, the error does not occur.

The following is a code example:
DEFINT A-Z
PRINT "OUTPUT:  "
CALL RECURSION(5)
SUB RECURSION(5)     ' Dynamic subprogram
  IF MAX <> 0 THEN
     FOR I=1 TO MAX
       PRINT I;
     NEXT I
     PRINT
     MAX=MAX-1
     CALL RECURSION(MAX)   ' calls itself until max = 0
   END IF

END SUB
				
This is the output of the program:
OUTPUT:
 1  2  3  4  5
 1  2  3  4
 1  2  3
 1  2
 1
				

Modification Type: Minor Last Reviewed: 1/8/2003
Keywords: KB31888