#163829 - 2006-06-29 08:33 PM
GOSUB w/o Return?
|
smwells
Lurker
Registered: 2006-06-29
Posts: 2
|
I am revamping a logon script, and I noticed they fail to use 'return' at the end of each section, I am wondering if a 'return' is forced when the script hits the next section marker or does the script continue into the next area?
Also they call another gosub from the first, then the 2nd calls 3rd, and so on, but no return statment, instead they gosub to 'exit' subroutine..
|
|
Top
|
|
|
|
#163832 - 2006-06-29 08:51 PM
Re: GOSUB w/o Return?
|
smwells
Lurker
Registered: 2006-06-29
Posts: 2
|
I am planning a complete rewrite, but need to walk the existing one to see how it works.. (works.. hmm, a very grey area!!!)
|
|
Top
|
|
|
|
#163833 - 2006-06-29 08:56 PM
Re: GOSUB w/o Return?
|
Bryce
KiX Supporter
   
Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
|
Quote:
I am planning a complete rewrite, but need to walk the existing one to see how it works.. (works.. hmm, a very grey area!!!)
Code:
? "hello 1" gosub 1 ? "hello 2" ? "hello 3" 1: ? "hello 4" 2: ? "hello 5" gosub exit ? "hello 6" ? "hello 7" ? "hello 8" :exit
take the above code...
when ran... you get
hello 1 hello 4 hello 5
|
|
Top
|
|
|
|
#163836 - 2006-06-30 10:29 AM
Re: GOSUB w/o Return?
|
Richard H.
Administrator
   
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
Quote:
I am wondering if a 'return' is forced when the script hits the next section marker
No, there is no implicit return.
If the return is missing the script will drop through and execute the next section.
Also, if there is no exit in the main code, then the script will drop into the subroutines and exit them.
There are some uses for this technique, but they are exceptional and because they make the script so hard to understand and maintain they should be avoided.
Here is a very simple example of your current scenario: Code:
"In main" ? GoSub "FOO" GoSub "BAR" ; Subroutines :Foo "Now in FOO" ? :Bar "Now in BAR" ? RETURN
The coder is expecting to see "In Main", "Now in FOO" and "Now in BAR" but what we actually get is: Code:
In main Now in FOO Now in BAR Now in BAR Now in FOO Now in BAR
|
|
Top
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
1 registered
(Allen)
and 781 anonymous users online.
|
|
|