#73126 - 2003-02-15 06:34 PM
Function return values
|
Glenn Barnas
KiX Supporter
   
Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
|
Hi all!
Not sure if here or UDFs is the better place to post this, but - here goes.
After looking at the Kix manual, other people's UDFs, and the FAQ, I'm not really clear on the BEST way to code the value returned from a UDF.
Some examples I've considered: INGROUP: 1 if found, 0 if not EXIST: 1 if found, 0 if not OPEN: 0 if success, other if error
Basically, what I've seen is that if a function "looks for something" (exist/not exist) it returns "1" on success, zero otherwise. However, if a function performs some other action, it appears to reverse the return values - "0" on success, non-zero for failure.
Is this a fair assumption?
How might you return an error code when the function normally returns an array? Is "EXIT 1" when an error occurs enough? Or should I end every function with an "EXIT 0" ? See below:
code:
Function test() Open(some_file) If @ERROR Exit @ERROR EndIf : do stuff to the file & Close() : $test = $answer1,$answer2 ; return an array Exit 0 ; <= is this necessary, or recommended? EndFunction
Finally, when a function can return Yes, No, or Fail, is "1", "0", and "-1" a reasonable representation of the Yes, No, and Fail values?
Thanks for the comments & feedback.. I kind of know what's right in my head, but - if I'm going to write a UDF that might be used by others, I'd like to make sure I follow the same path as everyone else (within reason ).
Glenn
_________________________
Actually I am a Rocket Scientist!
|
|
Top
|
|
|
|
#73127 - 2003-02-15 06:57 PM
Re: Function return values
|
Howard Bullock
KiX Supporter
   
Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
|
You can look at my TranslateName to see how I decided to handle the return of both data and error info in an array. This has not been really accepted but was necessary to understand the error when the TranslateName UDF was called from within another UDF.
The OPEN example you sited used the DOS like format where 0 = success and any number above 0 is the error code.
When a UDF return data, I try to find the DOS error number that best fits the error I trapped and use "Exit 5" for access denied or Exit 87 (The parameter is incorrect.) for passing in bad parms. Normal exits would be "Exit 0".
When the "Exit" is used in the UDF the calling program can then check @ERROR to determine what happened. [ 15. February 2003, 19:02: Message edited by: Howard Bullock ]
|
|
Top
|
|
|
|
#73128 - 2003-02-15 07:01 PM
Re: Function return values
|
Howard Bullock
KiX Supporter
   
Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
|
Also if you look at my OpenFile() UDF, you will find and example of using "Exit" to make the actual error codes available to the calling program.
|
|
Top
|
|
|
|
Moderator: Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart
|
1 registered
(Allen)
and 1607 anonymous users online.
|
|
|