Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > return statement at end of function

Reply
Thread Tools

return statement at end of function

 
 
Marlene Stebbins
Guest
Posts: n/a
 
      01-28-2005
My program has code like this:

bigint bigSub(bigint minuend, bigint subtrahend)
{
bigint bigdiff;

bigInit(&bigdiff);

if(((cmp_ops(minuend.number, subtrahend.number))==1) &&
minuend.sign == POS && subtrahend.sign == POS)
{
bigdiff = abs_sub(minuend, subtrahend);
bigdiff.sign = POS;
bigdiff.size = strlen(bigdiff.number);
return bigdiff;
}
else if(((cmp_ops(minuend.number, subtrahend.number))==0) &&
minuend.sign == POS && subtrahend.sign == POS)
{
bigdiff = abs_sub(subtrahend, minuend);
bigdiff.sign = NEG;
bigdiff.size = strlen(bigdiff.number);
return bigdiff;
}
... /* several more if() blocks

/* no final return statement */
}

Some compilers don't complain at all; some issue warnings about missing
return values and some generate an error, interpreting the return value
as an int. What does the C standard say about this and what is standard
practice? I could use a goto, but...
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      01-28-2005
Marlene Stebbins <(E-Mail Removed)> writes:
> My program has code like this:
>
> bigint bigSub(bigint minuend, bigint subtrahend)
> {
> bigint bigdiff;
>
> bigInit(&bigdiff);
>
> if(((cmp_ops(minuend.number, subtrahend.number))==1) &&
> minuend.sign == POS && subtrahend.sign == POS)
> {
> bigdiff = abs_sub(minuend, subtrahend);
> bigdiff.sign = POS;
> bigdiff.size = strlen(bigdiff.number);
> return bigdiff;
> }
> else if(((cmp_ops(minuend.number, subtrahend.number))==0) &&
> minuend.sign == POS && subtrahend.sign == POS)
> {
> bigdiff = abs_sub(subtrahend, minuend);
> bigdiff.sign = NEG;
> bigdiff.size = strlen(bigdiff.number);
> return bigdiff;
> }
> ... /* several more if() blocks
>
> /* no final return statement */
> }
>
> Some compilers don't complain at all; some issue warnings about missing
> return values and some generate an error, interpreting the return value
> as an int. What does the C standard say about this and what is standard
> practice? I could use a goto, but...


As long as there's no way to reach the final '}' without executing a
return, there's no real problem. If any of the if blocks are missing
a return statement, there is a problem. (I actually can't find the
passage in the standard that says what happens when a non-void
function terminates without executing a return statement.)

Some would argue that there should only be a single return statement
at the end of the function, but that's a matter of style.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <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.
 
Reply With Quote
 
 
 
 
S.Tobias
Guest
Posts: n/a
 
      01-28-2005
Keith Thompson <(E-Mail Removed)> wrote:

> (I actually can't find the
> passage in the standard that says what happens when a non-void
> function terminates without executing a return statement.)


# 6.9.1 Function definitions
#
# 12 If the } that terminates a function is reached, and the value of
# the function call is used by the caller, the behavior is undefined.

--
Stan Tobias
mailx `echo (E-Mail Removed)LID | sed s/[[:upper:]]//g`
 
Reply With Quote
 
Chris Torek
Guest
Posts: n/a
 
      01-29-2005
In article <pmyKd.196917$Xk.60264@pd7tw3no>
Marlene Stebbins <(E-Mail Removed)> wrote:
>My program has code like this:


[much snippage, and some reformatting for vertical space]

>bigint bigSub(bigint minuend, bigint subtrahend) {

...
> if (...) {

...
> return bigdiff;
> } else if (...) {

[etc]
> return bigdiff;
> }
> /* no final return statement */
>}
>
>Some compilers don't complain at all; some issue warnings about missing
>return values and some generate an error, interpreting the return value
>as an int. What does the C standard say about this and what is standard
>practice? I could use a goto, but...


The C standard has little to say about it -- the code is correct
as long as the "missing" (not really missing, just some compilers
think so) return statement is never reached.

In cases like this, I prefer to restructure the code, giving
something like either of these:

f_type f(args) {
...
if (...) {
...
return result;
}
if (...) {
...
return result;
}
/* at this point only one final case remains */
if (!final_case)
panic("f(): impossible situation");
...
return result;
}

or:

f_type f(args) {
...
if (...) {
...
} else if {
...
} else if {
...
} else {
panic("f(): impossible situation");
}
return result;
}

Obviously this second version can only be used if the "result"
variable (bigdiff in your original code) is always the expression
being "return"ed.

The middle version (my first version above) has the slight drawback
that the code is asymmetric -- the final case is not as indented
as the remaining ones.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (4039.22'N, 11150.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
function Object,function statement,function operator alex Javascript 3 12-28-2006 02:57 PM
A value returned to a function without a `return' statement, why? lovecreatesbeauty C Programming 8 03-15-2005 08:00 PM
what value does lack of return or empty "return;" return Greenhorn C Programming 15 03-06-2005 08:19 PM
Can a function return statement be elided? Michael Klatt C++ 2 09-19-2004 04:44 PM
getting return value from function without return statement. Seong-Kook Shin C Programming 1 06-18-2004 08:19 AM



Advertisments