02-04-2024, 06:13 PM
(02-04-2024, 04:52 PM)bartok Wrote:(02-04-2024, 03:40 PM)bplus Wrote: A Gosub is part of the main or subroutine as they share variables with the main or subroutine depending where they reside but they are put off into their own little private corner to run a repetitious bit of code over and over without interferring with the main or subroutine.I dont' have understood you answer, compared to my question. I try to be clearer. I know the difference between the real SUB, that is generally CALLed by mean of CALL (even if in QB64 is not compulsory, but I use CALL) and the "sub" called by GOSUB. And I think to have clear the way in which the values are passed in a SUB, as I know that a "sub" in a kind of extention of the code, with which it shares all.
That is one main difference between a Gosub and "real" sub or function, the GoSub uses or shares variables with the main or subroutine it resides in. A "real" sub or function uses it's own set of variables different from the Main code set of variables. The other difference is that a "real" sub or function can have variables shared with main through the call eg, mySub var1, var2, ... whereas you only call a Gosub: myGosub the name alone.
A variables or array declaration section in a GOSUB can be moved to the top of Main or Subroutine and should be so you don't redeclare the variables and arrays each time the gosub is used by the main or subroutine, that's redundant and inefficient.
And another difference between a GoSub and a "Real" sub or function: The "real" subs or functions can be called by either another sub or function, the main program or even by a gosub part of main or subroutine. "Real" subs or functions are like completely independent little programs, you can even copy then and use them in other programs! That's the beauty of a really good Sub or Function and thus you can build Libraries of Subs and Functions, say for advanced array handling or file stuff or even GUI's or little interpreters.
What I thought was that in a SUB, a "sub" called by means of GOSUB works - into the SUB - in exactly the same way in which works a "sub" called by means of GOSUB, into the main code of the program . As to say, I thought that - into a SUB - a "sub" caled by means of GOSUB shares automatically all that was declared into the main code of the SUB.
Returning to the example, let's take a look at this:
Case A)<------------------------------------------
OPTION _EXPLICIT
'[variables declaration]
'[arrays declaration]
[main code of the program in which variables and arrays declared are not used]
GOSUB test:
END
test:
[variable declaration]
[arrays declaration]
[code of the "sub" in which variables and arrays declared are used]
RETURN
In this case (A), the program works in both ways: declaration at the beginning, or declaration into the "sub"
Case B)<------------------------------------------
OPTION _EXPLICIT
[main code]
CALL subroutine
END
SUB subroutine
[variable declaration of the SUB]
'[arrays declaration of the SUB]
[main code of the SUB in which variables and arrays declared are not used]
GOSUB test:
test:
'[variable declaration of the SUB]
[arrays declaration of the SUB]
[code of the "sub" in which variables and arrays declared are used]
RETURN
END SUB
In this case (B), it doesn't work in both ways:
[variable declaration of the SUB]: it can be at the beginning of the SUB, or at the beginning of the "sub", it's the same.
[arrays declaration of the SUB]: it works only if it is at the beginning of the "sub". If I put it at the beginning of the SUB, I get an OUT OF RANGE error. Not error about OPTION _EXPLICIT.
I'd like to see the code you are saying you can't declare the array right at the beginning and MUST declare it in the GoSub. I strongly suspect something else is going on that you might be misinterpreting that the array MUST be declared after the GOSUB line label ??? You got me really curious Don't get me wrong if you don't use the array elsewhere in the sub or main code that the GoSub is in, it would likely work. But really let's see a real example.
I could make one up but it would either be irrelevant to you, not understandable or completely trivial. So let's see what specific example you are seeing this, please.
b = b + ...