Page 1 of 1 1
Topic Options
#37243 - 2003-03-04 07:10 AM Creating UDF
JohnQ Offline
Starting to like KiXtart

Registered: 2003-03-04
Posts: 171
When creating a UDF... if you have an IF statement and the desired condition is not met, what is the best way to abort the function and either exit KIX altogether or return to the main part of the script? EXIT or QUIT do not seem to stop the function from continuing.
Top
#37244 - 2003-03-04 07:57 AM Re: Creating UDF
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
John,

welcome to the board !

Exit does exit the function, Quit does quit the script ...

Let us see what you have
_________________________



Top
#37245 - 2003-03-04 08:58 AM Re: Creating UDF
JohnQ Offline
Starting to like KiXtart

Registered: 2003-03-04
Posts: 171
I was using EXIT by itself and if the previous condition was not met, the function would continue. If I use EXIT 0 or EXIT 1 it works as you stated. I know it's simple and stupid, but can you explain the difference?
Top
#37246 - 2003-03-04 09:21 AM Re: Creating UDF
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
Excerpt from the manual :

quote:
Exit

Action: Exits the current KiXtart script, or, if used at the topmost level, exits KiXtart.

Syntax: EXIT [error level / exit code]

Remarks: If EXIT is followed by a numeric expression, then @ERROR is set to the value of that expression and you can check it in the calling script or batch file.


Exit as is tries to get the next statement as error condition , so if this is an else or an endif it might happen that the parser ignores this and continues in the subsequent (Just a guess as I don't exactly know what happens)

However, some of us (including me) code an exit statement where nothing is needed to return as

exit()

Some place for confusion as exit is a command and not a function [Wink] , but it then exits as you supposed without continuing or setting @error

hth
_________________________



Top
#37247 - 2003-03-04 09:23 AM Re: Creating UDF
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
It's a kind of bug.

Despite what the manual appears to say, you should always use a value with Exit (and Quit).

The problem is that the parser has been known to exhibit odd behaviour when there is no value - it swallows the next token/keyword which is probably your EndIf.

This means that your If statement is now badly constructed and the parser has a hell of a job working out what is going on.

In short - always supply a value for Exit and Quit - there is no good reason not to.

This feature lead to the infamous "Exit Exit" kludge [Wink]

Top
#37248 - 2003-03-04 09:30 AM Re: Creating UDF
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
or quit quit [Big Grin]
_________________________



Top
#37249 - 2003-03-04 09:37 AM Re: Creating UDF
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11631
Loc: CA
Hmmm... JohnQ, I'm not as far along in programming as these boys, but using the RETURN "appears" to work fine for me. Can be hard to keep track of the RETURNS though and lose your way.
Top
#37250 - 2003-03-04 09:44 AM Re: Creating UDF
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
quote:
or quit quit
Ah. I changed the name to protect the innocent.

[ 04. March 2003, 09:44: Message edited by: Richard H. ]

Top
#37251 - 2003-03-04 03:25 PM Re: Creating UDF
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
code:
$a=''
$rc=test1()
? 'a'=$a
? 'rc='+$rc
? ''+@ERROR+' - '+@SERROR
$rc=test2()
? 'a'=$a
? 'rc='+$rc
? ''+@ERROR+' - '+@SERROR
function test1($a)
$test1=''
if not $a
return
else
; stuff
endif
endfunction
function test2($a)
$test2=''
if not $test2
exit 87
else
;stuff
endif
endfunction

Difference:
The first function call does not set an appropriate rerror code whereas the second call rteturns an appropriate error code which canthen be tested for.
_________________________
There are two types of vessels, submarines and targets.

Top
Page 1 of 1 1


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

Who's Online
0 registered and 302 anonymous users online.
Newest Members
Sir_Barrington, batdk82, StuTheCoder, M_Moore, BeeEm
17886 Registered Users

Generated in 0.058 seconds in which 0.022 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