3 ms·
This is kinda recursion - stackless, though - so it doesn't work right (and honestly, I don't know if it would work properly on actual BASIC from the era or not
by cr0sh 8y ago
This is kinda recursion - stackless, though - so it doesn't work right (and honestly, I don't know if it would work properly on actual BASIC from the era or not; it would depend on how the GOSUB/RETURN stack worked - I don't recall if you can GOSUB to a routine line number prior to the GOSUB line number - not to mention other reasons):
10 Y0=1:GOSUB 1000:PRINT Y0
999 END
1000 REM
1010 Y0=Y0+1
1020 IF Y0<5 THEN GOSUB 1000
1030 RETURN
You could potentially make something work with a DIM array for a limited sized stack to make "local" variables of some sort for each level of recursion. It would be very ugly - but barely possible I think.
This seems like a fun challenge to try out...
- mntmoss 8y agoMany of the languages of the era, BASIC dialects included, just wouldn't recurse. You could call a single subroutine and return from that one, but it would either terminate with an error or overwrite the first return address if you tried to use it like a stack. So you would have to manage the entire thing manually - addresses and variables both. With enough POKE and PEEK you could certainly rig up a solution. (C having a callstack is actually a pretty big deal, and not everyone adapted to it well at the time.)
- rbanffy 8y agoYou need to keep a set of global pointers to arrays for all internal state of the function, increment it on the entry point and decrement it before the subroutine's RETURN. It's a nightmare, but doable.
- tom_ 8y agoWhich dialects?? A stack is the obvious thing to have. You'll already need something like for nested FOR loops, so it's the obvious thing to do for GOSUB as well. I'd be surprised to hear that any non-toy implementations didn't have a GOSUB/RETURN stack. ZX81 BASIC has a GOSUB/RETURN stack. MS BASIC has a GOSUB/RETURN stack. Atari BASIC has a GOSUB/RETURN stack. Apple 1 BASIC has a GOSUB/RETURN stack! - sure, it appears to be all of 8 deep, so good luck with using it for anything recursive, but it's a stack nonetheless...