Re: return two values at once from C function?
Re: return two values at once from C function?
- Subject: Re: return two values at once from C function?
- From: Ronald Oussoren <email@hidden>
- Date: Fri, 29 Aug 2003 17:43:07 +0200
On vrijdag, 29 augustus 2003, at 16:42PM, David Thorp wrote:
Greetings again,
I'm now wondering if I SHOULD make the function in my previous email
handle the errors. The problem is...
I've probably missed something really obvious, but I'm trying to
figure out if there's any way to return more than one value from a
function. In particular this is relevant when I'm trying to return an
error code along with another integer or something.
What if say, I have a function that returns an integer, but if there's
an error, the function says to return null. Does that actually make
any sense? Or is that breaking something, or whatever?
You can use a specific value to indicate an error return. This is what
a lot of system calls do, e.g. read() return the number of bytes read
or -1 when there was error. You could also use a output argument where
you write a status code (or the return value):
double mySqrt(double value, int* status)
{
if (value < 0.0) {
*status = ERROR;
return 0.0;
} else {
*status = OK;
// Calculate a result here
return result;
}
}
You could also use exceptions, although that seems to be not 'The Right
Way' in Objective-C.
Ronald
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.