i have this main script and subs as follows. Is Sub within Sub an accepted practice?:
Main script
...
...
GoSub, L
...
Return
L:
L script
...
...
GoSub, L2
...
Return
L2:
L2 script
...
...
...
Return
Best Answer Lexikos , 23 October 2015 - 11:35 PM
i have this main script and subs as follows. Is Sub within Sub an accepted practice?:
Main script
...
...
GoSub, L
...
Return
L:
L script
...
...
GoSub, L2
...
Return
L2:
L2 script
...
...
...
Return
It's fine, but can get a little confusing. Just be sure of where your returns are sending you. If possible I recommend functions over SubRoutines. Helps keep your variables from getting mucked up and can simplify the flow (sometimes). Honestly most anything will work, just how readable it is is up to you.
In different programming languages, a subroutine may be called a procedure, a function, a routine, a method, or a subprogram. The generic term callable unit is sometimes used.
Source: Subroutine - Wikipedia, the free encyclopedia
"sub within a sub" is not a good practice, and will not even function correctly if your return
s are misplaced.
however, the example you've given is not a "sub within a sub". your example is merely one sub calling out to another sub, which as Lexikos said is normal
here is what i would consider "sub within a sub":
;main script GoSub, L1 return L1: ;L1 code ; L2: ;L2 code ; ;more L1 code return
in that case, L1 would fall into L2 and keep executing until the next return
best to use functions instead of subroutines to avoid this confusion
thanks for the clarification and an additional example of a true "sub within sub".