SuperBASIC Recursion

See all articles from CATS v8 n4
Author(s): Tim Swenson
Publication: CATS Newsletter
Pub Details: CATS v8 n4 (Volume 8, Issue 4)
Date: September 1990
Pages: 6

Recursion is the use of a program that calls itself. Jokingly one could say that the dictionary definition of recursion is; Recursion see Recursion.

Recursion is one programming practice that has normally not been available to BASIC. Since SuperBasic is known for going beyond normal BASIC, it does support recursion.

Recursion is used to break up a problem into smaller chunks and write a procedure that will handle any of the chunks. The best way to describe recursion is to show you how it works. Below is a program that will recursivly split a string in half. The program will print out the string as it works on it. Also look at Figure 1. You will notice that the program seems to cover the tree using a left hand rule.

Recursion is one of the best ways to handle tree structures. Trees are one of the major structures discussed in most Data Structure classes.

The idea I’m trying to convey is that a procedure can call itself before it has reached the end of the procedure. When a procedure calls itself, it takes a “picture” of the current variables and pushes then on a stack. It then takes the new values passed it and executes using them. If it calls itself again, it pushes again. When a return statement is encountered, the procedure pops the last values off the stack and continues where it left off.

You may not find recursion usefull in your programming, but if you are translating from a language that supports it, it makes it easier to translate to SuperBasic.

10 DEFine PROCedure split(in$)
20 LOCal temp
30 PRINT in$
40 IF LEN(in$)=1 THEN RETurn
50 temp=INT(LEN(in$)/2)
60 split in$(1 TO temp)
70 split in$(temp+1 TO
LEN(in$))
80 END DEFine split
80 END DEFine split
Figure 1. Recursion tree splitting abcdefg
Figure 1. Recursion tree splitting abcdefg

Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.