Self-initializing subprograms

1

SP4CEBAR 2025-03-17 21:26 (Edited)

The code below has a subprogram that returns A and B, but when calling it, they don't have to be initialized

CALL MAIN
END

SUB MAIN
  GOSUB TEST
  PRINT A, B
END SUB

TEST:
  A= 0
  B= 0
  CALL TEST( A, B )
RETURN

SUB TEST( A, B )
  A= 1
  B= 2
END SUB


was8bit 2025-03-18 20:44 (Edited)

TEST:
A= 0
B= 0
CALL TEST( A, B )
RETURN

This initializes the variables


When you call
… GOSUB TEST

The code in any GOSUB that was called inside to at SUB is “copied” into that sub as if it was literally typed into that sub

Where it gets confusing is when different SUBS or even GOSUBS calm the same subroutine, as settings from one calling will not interfer with other calls for other subs..

… this can get confusing, so care must be take if you are using the same code for different SUBS..


was8bit 2025-03-18 20:48

The values in a SUB call connects and effects the variables used in calling the SUB

So the AB variables are not necessarily the same variables overall


was8bit 2025-03-18 20:50

So you can use a SUB to perform changes to any inputted variable…


SP4CEBAR 2025-03-18 21:49

The code in any GOSUB that was called inside to at SUB is “copied” into that sub as if it was literally typed into that sub

this sums it up it really well

I think if a subroutine gets messy you can add a comment that lists all local variables used


was8bit 2025-03-19 05:03

As i get easily confused with too many things going on at once, i tend to break down each sub to a simple but useful task... kinda like creating a simple function, like handling movement in a maze for any player or enemy... i then name the sub simply... ex. MOVE

But, in a SUB, you can actually declare and use arrays etc. that all only exist during that sub's execution... ;)


Timo 2025-03-19 16:31

Calling GOSUB or GOTO inside of a SUB to jump outside of it, is calling for trouble ;)
It should show an error, but it was too complicated to implement...


was8bit 2025-03-20 05:20

I have, with great care, have used gosub from inside a SUB on rare occasions… but ONLY if I find I am repeating a batch of identical code several times in the SUB…

So in this case it’s like I HAVE witten the same code over and over, but the GOSUB then behaves like shorthand so in don’t have to actually write out the same code over and over again…

… it isn’t often I need this, but it is handy to do when the need arises ;)



Goto from within a SUB can cause odd behavior and even crash or stack overflow

Use nested WHILE WEND if you want to have varied conditional processing inside a SUB

And anytime you have a bunch of code all shooshed together and you find it confusing … GOSUB is a great way to isolate sections of code off to the side so you better see what is going on overall…

Some advanced codes I see are all concisely written, and they play well… i just cannot make “heads or tails” of them…

… so i am happy to have the option of a GOSUB when I need it ;)





Log in to reply.