Page 2 of 2 <12
Topic Options
#47170 - 2003-10-23 08:33 AM Re: Using exit from a within gosub and do...until?
sneal Offline
Fresh Scripter

Registered: 2003-10-22
Posts: 6
Loc: Tokyo, Japan
Thanks for Shawn's suggestion,
quote:
I think the real solution to this, and better programming practice, is that one should never want to really exit from within a UDF or sub-routine anyways ... should always exit from the mainline ...

At first, I was ticked at the criticism of my programming, [Mad] but his criticism gave me the solution to my problem: in 3.63, I can set an error variable in the subroutine, and check the variable in the mainline, thus exiting without an error (I've removed the progress checking).
code:
do
gosub "subscript"
if $SubErr
exit 1
endif
until 1 = 1
exit 0
:subscript
$SubErr = 1
return

Always trying to improve my listening skills...
Sam

Top
#47171 - 2003-10-23 09:06 AM Re: Using exit from a within gosub and do...until?
MightyR1 Offline
MM club member
*****

Registered: 1999-09-09
Posts: 1264
Loc: The Netherlands
Could it be an incorrect usage of exit???

Like the manual says about exit:
quote:

Exits the current KiXtart script, or, if used at the topmost level, exits KiXtart. Exit can also be used to leave a UDF.

and about return:
quote:

Causes script execution to continue at the statement following the last CALL or GOSUB statement, and also returns from inside a UDF.

and about gosub:
quote:

When a RETURN statement is encountered, script execution continues at the statement following the GOSUB statement.

so following the manual, exit should not be used in a subroutine...
_________________________
Greetz,
Patrick Rutten

- We'll either find a way or make one...
- Knowledge is power; knowing how to find it is more powerful...
- Problems don't exist; they are challenges...

Top
#47172 - 2003-10-23 11:00 AM Re: Using exit from a within gosub and do...until?
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
quote:
so following the manual, exit should not be used in a subroutine...
Why not? The quotes imply both that it can be used in a subroutine, and that it should exit the script. Nothing implies that it cannot.

Having said that, the manual is an evolving document which is frequently contrary and lacking in detailed information.

I'll reiterate the samples I posted earlier with more comment:

; Sample 1
GoSub Sub

; Sample 2
For $i = 1 To 10 "In Loop" ? Exit 0 Next

; Sample 3
For $i = 1 To 10 "In Loop" ? Gosub Sub Next

:Sub
"Subroutine Called" ?
Exit 0


"Sample 1" works as expected. When the script hits the exit in the subroutine the script is exited immediately.

"Sample 2" works as expected. When the script hits the exit in the loop the script is exited immediately.

"Sample 3" does not work. When the script hits the exit processing returns to somewhere in the calling loop, not necessarily where you'd expect.

The problem is not using exit in a loop or in a subroutine, as both are valid and both work.

The problem is that there is some internal clean-up which is going awry when the subroutine is called from the loop.

Exiting a "CALLed" script works as you'd expect, like a "Return", where processing continues after the CALL statement.

quote:
But Exit x in subroutines, causes the loop to start from beginning of the loop, skipping the remainder of the loop
Not quite true. If it was (and was consistantly applied) it might make a useful feature, but waaaay back in sneal's original post you will see that his script actually abends.

sneal: I hadn't fully appreciated that you were using 3.63. Any fix for this issue will of course not be back-ported to 3.63, so this is your opportunity to upgrade [Smile]

I think "Exit" should work consistantly in a subroutine. I'm just as happy for it to return to the next statement following the gosub as exit the script (file). The action simply has to be consistant.

With the advent of UDFs, subroutines are no longer useful (at least I can't think of any good use for them) so I'm not going to be too concerned if they are deprecated and issues like this fixed by a change in the manual which states "Exit does not work within a subroutine".

The main reason for reporting it as a bug is that the internal clean-up code which is not working is likely to be generic, so may affect other areas.

Top
Page 2 of 2 <12


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 1114 anonymous users online.
Newest Members
Viginette, ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder
17888 Registered Users

Generated in 0.05 seconds in which 0.025 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org