KnowledgeBase Archive

An Archive of Early Microsoft KnowledgeBase Articles

View on GitHub

Q28165: "String Formula Too Complex" with Recursive String Function

Article: Q28165
Product(s): See article
Version(s): 6.00 6.00b 7.00 | 6.00 6.00b 7.00
Operating System(s): MS-DOS | OS/2
Keyword(s): ENDUSER | B_QuickBas | mspl13_basic
Last Modified: 2-FEB-1990

The program below, which recursively invokes a string function, causes
a "String Formula Too Complex" run-time error. This program shows a
design limitation in Microsoft BASIC Compiler Version 6.00 and 6.00b,
Microsoft BASIC Professional Development System (PDS) Version 7.00,
and in QuickBASIC that is not usually encountered.

In the following program, QuickBASIC is computing RIGHT$(TEXT$,1),
assigning the value to a temporary string descriptor, and then
recursing on REVERSE$(LEFT$(TEXT$,LEN(TEXT$)-1)):

   DECLARE FUNCTION Reverse$ (Text$)
   CLS
   PRINT Reverse$ ("123456789 1234567890")

   FUNCTION Reverse$ (Text$)
   IF LEN(Text$) = 0 THEN
     EXIT FUNCTION
   ELSE
     Reverse$ = RIGHT$(Text$, 1) + Reverse$ (LEFT$(Text$, LEN(Text$) -1))
   END IF
   END FUNCTION

The temporary string descriptor is not released until the recursion is
completed. Each recursive REVERSE$ function call locks one string
descriptor. Because QuickBASIC allows only 20 temporary descriptors
(more than enough for most applications), after around 20 function
calls the program gives the error "String Formula Too Complex."

The workaround is to use a real string descriptor as follows (which
will be allocated from stack space):

   Temp$ = Reverse$(LEFT$(Text$,LEN(Text$)-1))
   Reverse$ = RIGHT$(Text$,1) + Temp$

THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Copyright Microsoft Corporation 1986-2002.