Is the [error level / exit code] optional for the EXIT command?

Yes and No!

When KiX encounters the EXIT command, it processes what follows it as though it were an optional expression but if the optional expression is not provided, it processes whatever inline code it thinks to be the optional expression.

Take the following example:
Code:
Gosub test

MessageBox("second","test")
Exit

:test
MessageBox("first","test")
Return


The first test called in the GOSUB spawns a MesageBox, then the second since it is before the exit, but what some might not expect is that the first test is spawned yet again.


Even if we handle the return codes from the MessageBox() function, it still does the same. KiX does not know where to stop and goes on to enumerate expressions that follow the EXIT.
Code:
Gosub test

$=MessageBox("second","test")
Exit

:test
$=MessageBox("first","test")
Return



The solution is to ALWAYS include the not-so-optional value or expression.

EXIT 0
EXIT 1
EXIT @Error

When providing the parm, one should try to remain true to the meaning of the EXIT code. Also, remember that EXIT is used in different places with different results. It does NOT always EXIT KiX. If you truly wish to EXIT KiX, use the QUIT command but don't forget to pass a parm to is as well!

The value passed to EXIT and/or QUIT will set the environment %ERRORLEVEL% to that value when it EXITs KiX. Knowing that, if KiX is called via a BATch file, there can be some error logging and/or recovery. See the FAQ Catch that error message that flashes for a second

BTW, did you know that MessageBox() will set @Error? It can really throw your code for a loop if you LOOP on @Error=0! That is a topic for another FAQ, but while I am here I may as well share the following example of using EXIT to zero out @Error.
Code:
break on

$rc=MessageBox("first","test")
'Dang! look what MessageBox() does to @@Error... ' + @error ?
ZeroError
'See what ZeroError does... ' +@error ?
function ZeroError()
EXIT 0
endfunction




QUIT 0
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.