Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why does my Loop end after 11 Loops?
#55
(02-11-2023, 08:44 PM)Dimster Wrote: Well you inspired me to put my Basic books and Binders of notes aside and check it out on the internet. You are correct. While I could not find Recursion using Basic there is a lot with all the other computer languages. Recursion is much more complex than I thought. I'm using it as a substitute for a Do...Loop or For...Next which I think would be classified as Direct Recursion. Shame there is not a lot on the internet about the Basic language and teaching examples of Recursion in Basic and all various ways to use it.

The BASIC concept for recursion is very, very simple:  Have a routine that simply calls itself.  That's it.  That's all recursion is, in a nutshell.  If you have a routine which calls itself, then you have recursion.

Now, what does a routine need to successfully perform recursion?  3 basic things;

1) A starting point.
2) An end point.
3) To be limited in scope.  Recursion which occurs a million times would be best served by some sort of loop structure.  Each call to recursion uses a "chunk" of memory the size of your recursive sub/function (after all, it can't exit until it gets back from the recursive call), so if you repeat some process too many times, your program is going to crash when it runs out of memory available.

^But that's basically your only requirements to write a recursive routine.  Let me show a few examples for you:

Code: (Select All)
RecurseSubPrint 1, 20

SUB RecurseSubPrint (start, finish)
    PRINT start
    start = start + 1
    IF start <= finish THEN RecurseSubPrint start, finish
END SUB

Now here, I'm passing my variables via parameter.  There's a start point, and a finish point, and in this case it only runs 20 levels deep with recursion.  All checks pass, so this is a valid recursive routine.

Code: (Select All)
FUNCTION RecurseFunPrint (finish)
    STATIC count, total
    count = count + 1
    PRINT count
    IF count < finish THEN total = total + count + RecurseFunPrint(finish)
    RecurseFunPrint = total
END FUNCTION

Now here, I'm keeping the starting point STATIC (count) and passing my end point back to my recursive routine (finish), and this too is only going to be for 20 levels of recursion in my program.  Now note, this WORKS, but **it's only going to work ONCE**.  By making my start point STATIC here, there's absolutely no way to reset it.  Count will increment by one every time it's called, and it's got no way to reset it.  Why someone would want a recursive routine to behave in this manner, I have no clue, but that's what it's going to do here.

Start Point.  End Point.  Limited number of calls back to itself.

That's it.  That's all recursion is.  There's no fancy secret to it.  It's not complicated.  It's just a routine that calls itself a limited number of times.



Now, with that said, many recursive routines work without having to pass a start or finish point.  HOW??  By whatever they're doing being constrained by the program itself in some other manner.  A tile program might always start at point (0,0) and increment by 1 until it goes all the way right and down until it runs out of screen space, like this little tile demo below:

Code: (Select All)
$COLOR:0
CLS , 0
RecurseTile Red, Blue
SLEEP
CLS , 0
RecurseTile Magenta, Cyan
SLEEP

SUB RecurseTile (Kolor1 AS _UNSIGNED LONG, Kolor2 AS _UNSIGNED LONG)
    x = POS(0)
    y = CSRLIN
    IF SCREEN(y, x, 1) \ 16 <> 0 THEN EXIT SUB
    IF (x + y) MOD 2 THEN COLOR Kolor1, Kolor1 ELSE COLOR Kolor2, Kolor2
    PRINT " ";
    IF x < _WIDTH THEN x = x + 1: LOCATE y, x: RecurseTile Kolor1, Kolor2
    IF y < _HEIGHT THEN y = y + 1: LOCATE y, 1: RecurseTile Kolor1, Kolor2
END SUB

Your recursive file access works just like this -- start point is the beginning of the file.  increment is by file record.  finish is EOF.  <-- Start.  Stop.  Limited recursion.

See how simple this stuff is, once you break it down to the BASICs?
Reply


Messages In This Thread
Why does my Loop end after 11 Loops? - by Dimster - 02-06-2023, 07:08 PM
RE: Why does my Loop end after 11 Loops? - by SMcNeill - 02-12-2023, 02:05 PM



Users browsing this thread: 6 Guest(s)