473,473 Members | 1,456 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Return Value Practice

Hello,

In the case of returning an integer from a function to notify success or
error which does 0 customarily signify? For example:

int chk( int c ) {
...
}

int fn( int c ) {
if( chk( c ) == 0 ) {
printf( "Success\n" );
} else {
printf( "Error\n" );
}
return c;
}

Looking at chk( c ) == 0 and the flow of the if statement, to be in line
with common practice, is the statement correct as is? Or should it become
chk( c ) != 0?

Thanks - Lyle.
Nov 14 '05 #1
12 1518
Lyle Fetterly wrote:
Hello,

In the case of returning an integer from a function to notify success or
error which does 0 customarily signify? For example:


To the extent that there is a custom, 0 usually signifies success. If
you're unfamiliar with a particular library function, however, always
read the documentation before assuming anything.

--
================================================== ======================
Ian Pilcher i.*******@comcast.net
================================================== ======================
Nov 14 '05 #2
Lyle Fetterly wrote:

In the case of returning an integer from a function to notify
success or error which does 0 customarily signify? For example:


Since success is the lack of errors, and there can be multiple
forms and classes of error, it is practical to signal success with
a zero, and error details with non-zero. Assuming no other
constrictions on the returned value.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 14 '05 #3

Lyle Fetterly wrote:
Hello,

In the case of returning an integer from a function to notify success or error which does 0 customarily signify?


As the others have mentioned, it's typically success, basically meaning
no error code. It's also in line with the return from main(), where 0
is the same as EXIT_SUCCESS (which is not to say that EXIT_SUCCESS == 0
necessarily).


Brian

Nov 14 '05 #4
On Mon, 4 Apr 2005 11:51:01 -0500, "Lyle Fetterly" <le****@mts.net>
wrote:
Hello,

In the case of returning an integer from a function to notify success or
error which does 0 customarily signify? For example:
Often, 0 is success, simply because there may be more than one failure
return. If you can have return values of 0 through 7, it seems
unnatural to pick 4 to mean success.

OTOH, in cases where the return is logical (true/false) instead of
numeric, you might well want 0 to mean "fail" and anything else to
mean "succeed." Look at the ctype functions, for example - isdigit()
returns 0 if its argument "fails" the test.

Write for readability.
int chk( int c ) {
...
}

int fn( int c ) {
if( chk( c ) == 0 ) {
printf( "Success\n" );
} else {
printf( "Error\n" );
}
return c;
}

Looking at chk( c ) == 0 and the flow of the if statement, to be in line
with common practice, is the statement correct as is? Or should it become
chk( c ) != 0?

Thanks - Lyle.


--
Al Balmer
Balmer Consulting
re************************@att.net
Nov 14 '05 #5
Alan Balmer wrote:
Often, 0 is success, simply because there may be more than one failure
return. If you can have return values of 0 through 7, it seems
unnatural to pick 4 to mean success.

OTOH, in cases where the return is logical (true/false) instead of
numeric, you might well want 0 to mean "fail" and anything else to
mean "succeed." Look at the ctype functions, for example - isdigit()
returns 0 if its argument "fails" the test.

Write for readability.


Are the constants

EXIT_SUCCESS
EXIT_FAILURE

useful here, when <stdlib.h> is included?

Nov 14 '05 #6


Jason Curl wrote:
Alan Balmer wrote:
Often, 0 is success, simply because there may be more than one failure
return. If you can have return values of 0 through 7, it seems
unnatural to pick 4 to mean success.

OTOH, in cases where the return is logical (true/false) instead of
numeric, you might well want 0 to mean "fail" and anything else to
mean "succeed." Look at the ctype functions, for example - isdigit()
returns 0 if its argument "fails" the test.

Write for readability.

Are the constants

EXIT_SUCCESS
EXIT_FAILURE

useful here, when <stdlib.h> is included?


They'll work on any "decent" implementation, but it's
probably better to avoid them for this use. The gaping
hole is that the two macros might expand to the same value
on an implementation where there's no way to communicate an
exit status to the environment. Also, if you're writing
code that could be useful in a free-standing environment,
keep in mind that <stdlib.h> might not exist at all.

--
Er*********@sun.com

Nov 14 '05 #7
> As the others have mentioned, it's typically success, basically meaning
no error code. It's also in line with the return from main(), where 0
is the same as EXIT_SUCCESS (which is not to say that EXIT_SUCCESS == 0
necessarily).


EXIT_SUCCESS / EXIT_FAILURE - yes, standard and intuitive. Thanks for
pointing them out.
Nov 14 '05 #8
On Tue, 05 Apr 2005 10:09:04 -0400, Eric Sosman
<er*********@sun.com> wrote:
Jason Curl wrote:

Are the constants

EXIT_SUCCESS
EXIT_FAILURE

useful here, when <stdlib.h> is included?


They'll work on any "decent" implementation, but it's
probably better to avoid them for this use. The gaping
hole is that the two macros might expand to the same value
on an implementation where there's no way to communicate an
exit status to the environment. Also, if you're writing
code that could be useful in a free-standing environment,
keep in mind that <stdlib.h> might not exist at all.


You would also need to test for them explicitly, because EXIT_SUCCESS is
not necessarily defined as zero. They are only designed as values to be
passed as the status to the exit() function:

If the value of status is zero or EXIT_SUCCESS, an implementation-
defined form of the status successful termination is returned. If the
value of status is EXIT_FAILURE, an implementation-defined form of
the status unsuccessful termination is returned. Otherwise the status
returned is implementation-defined.

Thus either zero or EXIT_SUCCESS is treated by exit() as a success
result but there is nothing to say that EXIT_SUCCESS has to be zero.

I would generally go with one of the following:

0 is OK, -1 is failure (and possibly anything else is a qualified
success). Used by the file I/O functions.

0 is false, 1 (or anything else) is true, used by the ctype.h
functions and macros.

-1, 0, +1 as comparison results, used by strcmp() etc.

An enumerated integer type (or possibly macros), usually in a header
file, for the exit conditions (MYFUNC_OK, MYFUNC_READFAIL,
MYFUNC_WRITEFAIL, ...).

Chris C
Nov 14 '05 #9
"Lyle Fetterly" <le****@mts.net> writes:
As the others have mentioned, it's typically success, basically meaning
no error code. It's also in line with the return from main(), where 0
is the same as EXIT_SUCCESS (which is not to say that EXIT_SUCCESS == 0
necessarily).


EXIT_SUCCESS / EXIT_FAILURE - yes, standard and intuitive. Thanks for
pointing them out.


For various reasons, it's not a good idea to use EXIT_SUCCESS and
EXIT_FAILURE as return codes for functions other than main(). It's
too easy to write code that assumes EXIT_SUCCESS is 0 and EXIT_FAILURE
is 1; such errors won't be found until the code is ported to a system
that uses different values. It's also conceivable that they could
have the same value. (Eric Sosman already pointed out some of this.)

There's no universal convention for return codes for functions. For
some functions, it makes sense to use 0 for "success" and non-zero
(typically -1) for "failure". Other functions, like the ones in
<ctype.h>, return a true/false result rather than a success/failure
result. Yet others might have a number of possible results, perhaps
encoded in an enum type.

Study the standard library and use its conventions where appropriate
(and not where they're not). Pick a consistent set of conventions for
a library of functions and stick with it. Make sure the caller and
callee use the same convention.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #10

Keith Thompson wrote:
"Lyle Fetterly" <le****@mts.net> writes:
As the others have mentioned, it's typically success, basically meaning no error code. It's also in line with the return from main(), where 0 is the same as EXIT_SUCCESS (which is not to say that EXIT_SUCCESS == 0 necessarily).
EXIT_SUCCESS / EXIT_FAILURE - yes, standard and intuitive. Thanks for pointing them out.


For various reasons, it's not a good idea to use EXIT_SUCCESS and
EXIT_FAILURE as return codes for functions other than main(). It's
too easy to write code that assumes EXIT_SUCCESS is 0 and

EXIT_FAILURE is 1; such errors won't be found until the code is ported to a system
that uses different values. It's also conceivable that they could
have the same value. (Eric Sosman already pointed out some of this.)


It should noted that I (the author the bit at the very top) was not
advocating the use of EXIT_SUCCESS as a return code for user-defined
functions, but merely showing that a return of 0 is one of the returns
from main() indicating success, hence is common enough in that context.

Brian

Nov 14 '05 #11
Chris Croughton wrote:
.... snip ...
I would generally go with one of the following:

0 is OK, -1 is failure (and possibly anything else is a qualified
success). Used by the file I/O functions.


Most file functions return EOF for failure, which is always
negative, and often is -1, but need not be.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson

Nov 14 '05 #12
On Mon, 04 Apr 2005 18:49:05 +0000, CBFalconer wrote:
Lyle Fetterly wrote:

In the case of returning an integer from a function to notify
success or error which does 0 customarily signify? For example:


Since success is the lack of errors, and there can be multiple
forms and classes of error, it is practical to signal success with
a zero, and error details with non-zero. Assuming no other
constrictions on the returned value.


But consider a funciton like getc(). It has many succcessful return values
and one vailure value. OK that failure value isn't zero (zero is a
valid success value) but I'm talking about the principle. Also consider
the relationship with pointers - 0 is a null pointer constant and a null
pointer return typically indicates a failure.

I think the point here is that there is no firm convention for this, it
can work perfectly well either way. All I would suggest is that if you are
creating a family of functions then it can help to be consistent within
the family.

Lawrence
Nov 14 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

94
by: John Bailo | last post by:
The c# *return* statement has been bothering me the past few months. I don't like the fact that you can have different code paths in a method and have multiple return statements. To me, it...
27
by: Maximus | last post by:
Hi, I was just wondering, is it good to use return without arguments in a void function as following: void SetMapLayer() { if( !Map ) return; layer = LAYER_MAP; }
7
by: zalzon | last post by:
Is it good practice in C++ for a member function to return data or is it better that data is stored in private member variable and printed in the member function? should i be using int...
15
by: Greenhorn | last post by:
Hi, when a function doesn't specify a return type ,value what value is returned. In the below programme, the function sample()is returning the value passed to 'k'. sample(int); main() { int...
15
by: Nerox | last post by:
Hi, If i write: #include <stdio.h> int foo(int); int main(void){ int a = 3; foo(a); }
5
by: Sam | last post by:
Hi All I have couple of question regarding property of a class and structures. **** ---- Here is my class and structure ---- ***** 1. Public Structure MyPoint 2. Dim p As Point 3. ...
66
by: Johan Tibell | last post by:
I've written a piece of code that uses sockets a lot (I know that sockets aren't portable C, this is not a question about sockets per se). Much of my code ended up looking like this: if...
3
by: Doug | last post by:
Hi i have a method that returns a value public bool readxml (string xmlFilename, out string value) but I would like to catch an exception if it occurs in the method . How do i catch the...
13
by: markn | last post by:
Running some code through static analysis, I noticed that gcc will generate a warning if a function returns an aggregate, controlled with this flag (from the gcc manual): -Waggregate-return...
1
by: djjohnst | last post by:
I am trying to fill out a form from a record set and have a few menu boxes that have a static list of values. I want to fill out the default value with the variable from the recordset. The one...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.