How To

Don't use GOTO or GOSUB to jump out of a subprogram!


nathanielbabiak 2023-09-22 21:52

Subject text appears in the manual. Obviously it's a core tenant of contemporary Basic, but I was wondering how it effects this fantasy console?

Especially since SUB-GOSUB works just fine?

Timo 2023-09-23 07:55

It will fill the stack of SUBs and eventually throw an error.
A SUB must always reach its END SUB.

nathanielbabiak 2023-09-23 18:33

Will that issue occur if the GOSUB reaches its RETURN, and control passes back to the subprogram, allowing it to reach its END SUB?

Also, do you think you'll ever modify the interpreter to prevent this type of thing? (I'm asking because it would cause issues with a bunch of uploads that rely on the current behavior...)

Timo 2023-09-23 20:44

In that case it might work!

I will not change any existing behavior of NX anymore. Maximum work I’ll do is fixing bugs. But even for this I don’t have time currently…

nathanielbabiak 2023-09-23 21:34

Awesome - sounds good!

was8bit 2023-09-24 01:50

If I recall, if you GOSUB to a batch of codes from a SUB and then RETURN back to the SUB, the code in the GOSUB is treated as if it is code inside the SUB, that is, protected variables...

I may test this later to verify, and also to see if the same GOSUB called from mainline code will treat it as the variables are mainline...

was8bit 2023-09-24 06:06

.... my test...

Sub Gosub Test.nx | Open in app
2023-09-24 06:07

was8bit 2023-09-24 06:09 (Edited)

So, GOSUB code can multitask as public core code as well as private SUB code...

Timo 2023-09-24 11:41

That's right, if you jumpt out of a SUB without END SUB, the interpreter still thinks it's inside, using its local variables.

was8bit 2023-09-24 12:33 (Edited)

Thats kinda a cool thing ;)

I would reckon that it would be common to reuse a GOSUB code either way... i could see one GOSUB code being reused by two or more SUBS, acting kinda like a FUNCTION maybe

... of course, reusing a GOSUB code in the main code is something ive done a few times... and i know ive actually used once a GOSUB code for two SUBS... in both cases it was because the code initially was repeated in more than one spot, then as i was editing and reediting the code it kept getting bigger and messier, it made it easier to move the code to one GOSUB code as editing errors between the two copies of code made it unmanageable...

... i cannnot remember ever having an issue using GOSUB code for one or the other, i use GOSUB rarely and have always used then exclusively... i can imagine one COULD get confused if they use it like i did in my test, however that was just a test and i doubt anyone would purposly set up and try to use such confusing code on purpose ;)

.... so, all that said, i am all for leaving the GOSUB with this flexibility...

SP4CEBAR 2023-09-26 06:18 (Edited)

I did some testing and found that all is fine as long as the program returns to a place within the subprogram it came from. If it encounters a new call to that same subprogram outside of the subprogram, it'll immediately throw a stack overflow error, but a call to any other function is fine, and ending the program is also fine

Jeanmilost 2023-10-01 07:31

I’m curious to know if a recursive function is well supported, or if it’s better to avoid such functions in LowResNX?

was8bit 2023-10-01 09:20 (Edited)

nathanielbabiak 2023-10-01 12:20

Recursion has limited support on this console, I've had to change program flow to avoid errors in the past.

SP4CEBAR 2023-10-01 14:36 (Edited)

This test shows that NX can recurse 128 times before throwing a stack overflow

was8bit 2023-10-01 17:39 (Edited)

I have sometimes used DO LOOP or DO WHILE that would repeatedly calculate infinitively on rare occasions, so i add a counter that boots out at a set amount of iterations, and add code to handle the boot-out...

This approach kinda works like a stack overflow handler, and avoids crashing ....

... usually happens when i am creating something randomly with specific targets, and occasionally the random results cannot resolve to meet all targets.... so i effectively "reboot" and restart the random generation ;)

Log in to reply.