When you use the Exit command in kix - either from a function or the main script, you pass the argument back to the calling environment, so

C:>kix32 test.kix
(command prompt, calls Kix32.exe with arg "test.kix")

$ = test('me!')
(Kix32 runs script - Script calls Test() UDF)

If $ID = 'me!' Exit 5 EndIf
(Test() decides it doesn't like you, so does Exit 5
(access denied) and thus sets the @ERROR macro.)

Logic in script checks Error macro and exits with same error
(back to next line after Test() call..)
If @ERROR
@SERROR ?
Exit @ERROR
EndIf

Back at the command prompt, the exit status of the script has been passed to the command interpreter, which can be viewed in the %ERRORLEVEL% variable.
Echo %ERRORLEVEL%

You don't need to CLEAR the error code, simply add the appropriate EXIT @ERROR line, so the error value from within the function is returned.

This works for simple functions like yours, but logic can change as they get more complex. Consider:
 Code:
Function Alive($Host)
 Shell '%COMSPEC% /c Ping ' + $Host + '>NUL:'
 If @ERROR
  $Alive = 0 ; return 0, not alive
  Exit @ERROR
 Else
  $Alive = 1 ; return 1, is alive
  Exit @ERROR ; 
 EndIf
EndFunction


With this kind of logic, you can do things like
 Code:
If Alive('some_computer')
 'Some_Computer is alive!' ?
Else
 'Error ' @ERROR ' occured while pinging Some_Computer!' @CRLF '(' @SERROR ')' ?
EndIF


Give it a try...

Glenn

PS - Note that this example sorta works, and is OK for explaining the logic, but is not a valid or reliable method of ping checks!


Edited by Glenn Barnas (2007-10-12 01:19 PM)
_________________________
Actually I am a Rocket Scientist! \:D