Page 1 of 1 1
Topic Options
#73126 - 2003-02-15 06:34 PM Function return values
Glenn Barnas Administrator Offline
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 [Big Grin] ).

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#73127 - 2003-02-15 06:57 PM Re: Function return values
Howard Bullock Offline
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 ]
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#73128 - 2003-02-15 07:01 PM Re: Function return values
Howard Bullock Offline
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.
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#73129 - 2003-02-15 07:12 PM Re: Function return values
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
The EXIT sets/resets the appropriate error code. Thus in a function
code:
function test($a)
;something happens
$test='returnvalue'
exit 0
endfucntion

The @ERROR macro will be actively set to '0' independent on whatever value it had before.
Alternatively, I can for example do an EXIT 87 to set the @ERROR macro to 87. I also try to mimic the regular KiXtart behavior as much as possible.
_________________________
There are two types of vessels, submarines and targets.

Top
#73130 - 2003-02-15 07:37 PM Re: Function return values
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
OK - thanks.. between your comments and getting away from the keyboard for a while to have lunch, I've pretty much decided that I'll return DATA via the "$fnNAME = value" and use the "EXIT #" to return success/failure status.

Some functions that are used to initialize data (and really can't fail) will now return nothing and simply "EXIT 0" at the end.

I "assume" that when I had omitted the EXIT 0 just prior to the EndFunction statement, that it defaulted to an @ERROR value of 0 after the function call. I'm going to explicitly place it there before I deploy or publish the routines.

Thanks again for the feedback..

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#73131 - 2003-02-15 08:15 PM Re: Function return values
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
You should always return a value in the DATA part, even if it is a '0' to indicate success. Never assume that a function will not fail.

My rule of thumb is: Always define everything and never assume anything [Wink]

[ 15. February 2003, 20:56: Message edited by: sealeopard ]
_________________________
There are two types of vessels, submarines and targets.

Top
Page 1 of 1 1


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

Who's Online
1 registered (Allen) and 1607 anonymous users online.
Newest Members
Viginette, ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder
17888 Registered Users

Generated in 0.055 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