473,403 Members | 2,354 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Redundant return

I have seen redundant return statements at the end of function. e.g.
void init_function(void)
{
a_init();
b_init();
c_init();
return;
}
I checked the generated code in GCC Compiler 3.2.2 on Red Hat Linux it
is ignored so it not harmful to use this.

Has anybody got any problem/issues with this on any compiler/platform?

Apr 12 '07 #1
11 1711
On Apr 12, 5:20 am, "quarkLore" <agarwal.prat...@gmail.comwrote:
I have seen redundant return statements at the end of function. e.g.
void init_function(void)
{
a_init();
b_init();
c_init();
return;
}
I checked the generated code in GCC Compiler 3.2.2 on Red Hat Linux it
is ignored so it not harmful to use this.

Has anybody got any problem/issues with this on any compiler/platform?

That's perfectly valid, if pointless. Any compiler that does not
handle it is broken.

Apr 12 '07 #2
On Apr 12, 6:25 am, "robertwess...@yahoo.com"
<robertwess...@yahoo.comwrote:
On Apr 12, 5:20 am, "quarkLore" <agarwal.prat...@gmail.comwrote:
I have seen redundant return statements at the end of function. e.g.
void init_function(void)
{
a_init();
b_init();
c_init();
return;
}
I checked the generated code in GCC Compiler 3.2.2 on Red Hat Linux it
is ignored so it not harmful to use this.
Has anybody got any problem/issues with this on any compiler/platform?

That's perfectly valid, if pointless.
Pointless? Maybe, maybe not. Certainly in this case no code is
produced.
On the other hand, a coding style in which you require an explicit
return
for all functions has advantages, especially if you use more than
one exit point.
- William Hughes
Any compiler that does not
handle it is broken.

Apr 12 '07 #3
quarkLore said:
I have seen redundant return statements at the end of function. e.g.
void init_function(void)
{
a_init();
b_init();
c_init();
return;
}
I checked the generated code in GCC Compiler 3.2.2 on Red Hat Linux it
is ignored so it not harmful to use this.
Right, and some people value its documentary effect. It reminds the
reader that this is a void function, stopping him from pointlessly
thinking "so where's the return value then?", and some people use it to
mean "DONE! Phew!" :-)
Has anybody got any problem/issues with this on any compiler/platform?
No, it's perfectly legal C and it does no harm whatsoever.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Apr 12 '07 #4
On 12 Apr, 11:20, "quarkLore" <agarwal.prat...@gmail.comwrote:
I have seen redundant return statements at the end of function. e.g.
void init_function(void)
{
a_init();
b_init();
c_init();
return;
}
To add to the comments already made, I would say that declaring a
function of type 'void' does not mean that it doesn't return, it means
that it doesn't return a value.

IMHO, 'Dropping off the end' of a function is just laziness, and from
a purely style point of view, I would always put an explicit return.

Apr 12 '07 #5
bytebro wrote:
On 12 Apr, 11:20, "quarkLore" <agarwal.prat...@gmail.comwrote:
>I have seen redundant return statements at the end of function. e.g.
void init_function(void)
{
a_init();
b_init();
c_init();
return;
}

To add to the comments already made, I would say that declaring a
function of type 'void' does not mean that it doesn't return, it means
that it doesn't return a value.

IMHO, 'Dropping off the end' of a function is just laziness, and from
a purely style point of view, I would always put an explicit return.
IMAO, putting an unnecessary return at the end of a function is just
waffling, and from a purely style point of view, I would always eliminate
such a return. It's just noise.

--
Jena user conference, September 2007: http://hpl.hp.com/conferences/juc2007/
"He's dead, Jim, but not as we know it." Unsaid /Trek/

Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England

Apr 12 '07 #6
On Apr 12, 9:49 am, Chris Dollin <chris.dol...@hp.comwrote:
bytebro wrote:
On 12 Apr, 11:20, "quarkLore" <agarwal.prat...@gmail.comwrote:
I have seen redundant return statements at the end of function. e.g.
void init_function(void)
{
a_init();
b_init();
c_init();
return;
}
To add to the comments already made, I would say that declaring a
function of type 'void' does not mean that it doesn't return, it means
that it doesn't return a value.
IMHO, 'Dropping off the end' of a function is just laziness, and from
a purely style point of view, I would always put an explicit return.

IMAO, putting an unnecessary return at the end of a function is just
waffling, and from a purely style point of view, I would always eliminate
such a return. It's just noise.
O boy a style war!

It's not just noise. Consider what happens if the function
undergoes multiple changes. The first change comes in response to
a bug where the program mysteriously crashes. Turns our that b_init()
will
crash if a_init fails. So the first change is

void init_function(void)
{
if(!a_init())
return;
b_init();
c_init();
}

things are OK for a while (no bugs have to be fixed), but a change in
OS means
that it is no longer easy to tell if a_init() failed or not. So a
change is
made so init_function returns SUCCESS or FAILURE

int init_function(void)
{
if(!a_init())
return FAILURE;
b_init();
c_init();
}

(Whoops, didn't catch the implicit return, the change should not
have been done by someone who assumed all returns were explicit.
Don't use that
contractor again.). Falling off now causes undefined
behaviour, which in this case is to return the value of a register,
which
happens to be non-zero, and tests false when compared to failure.
Things look fine. Until the next release of the compiler, when the
register now contains 0.
The fact that four programmers are involved does not help.
- William Hughes

Apr 12 '07 #7
Chris Dollin said:
bytebro wrote:
<snip>
>IMHO, 'Dropping off the end' of a function is just laziness, and from
a purely style point of view, I would always put an explicit return.

IMAO, putting an unnecessary return at the end of a function is just
waffling, and from a purely style point of view, I would always
eliminate such a return. It's just noise.
IMDO, can I just say that, whilst I disagree with Chris, it is clear
that the rules of C allow either approach, that neither approach is
dangerously flawed, and that we need not fall out over such minor
issues of style?

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Apr 12 '07 #8
Richard Heathfield wrote:
Chris Dollin said:
>bytebro wrote:
<snip>
>>IMHO, 'Dropping off the end' of a function is just laziness, and from
a purely style point of view, I would always put an explicit return.

IMAO, putting an unnecessary return at the end of a function is just
waffling, and from a purely style point of view, I would always
eliminate such a return. It's just noise.

IMDO, can I just say that, whilst I disagree with Chris, it is clear
that the rules of C allow either approach, that neither approach is
dangerously flawed, and that we need not fall out over such minor
issues of style?
Surely.

I was provoked by bytebro's accusations of "laziness"; it's not lazy
to avoid needless work.

--
JUC 2007, submit: http://hpl.hp.com/conferences/juc2007/submission.html
"Go not to the Elves for counsel, for they will answer both no and yes."/tLotR/

Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England

Apr 12 '07 #9
William Hughes wrote:
On Apr 12, 9:49 am, Chris Dollin <chris.dol...@hp.comwrote:
>bytebro wrote:
On 12 Apr, 11:20, "quarkLore" <agarwal.prat...@gmail.comwrote:
I have seen redundant return statements at the end of function. e.g.
void init_function(void)
{
a_init();
b_init();
c_init();
return;
}
To add to the comments already made, I would say that declaring a
function of type 'void' does not mean that it doesn't return, it means
that it doesn't return a value.
IMHO, 'Dropping off the end' of a function is just laziness, and from
a purely style point of view, I would always put an explicit return.

IMAO, putting an unnecessary return at the end of a function is just
waffling, and from a purely style point of view, I would always eliminate
such a return. It's just noise.

O boy a style war!
S'not a style war: it's an attitude war.
It's not just noise.
It's just noise /in that function/.
Consider what happens if the function undergoes multiple changes.
(fx:snip)

Sometimes bad things happen, especially without unit tests and
compilers that give helpful diagnostics. I'd hesitate to ascribe
the unwritten return as the dominant cause of the problem you
describe.

--
Jena user conference, September 2007: http://hpl.hp.com/conferences/juc2007/
"It is seldom good news." ~Crystal Ball~, /The Tough Guide to Fantasyland/

Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England

Apr 12 '07 #10
On Thu, 12 Apr 2007, Chris Dollin wrote:
Richard Heathfield wrote:
>Chris Dollin said:
>>bytebro wrote:
<snip>
>>>IMHO, 'Dropping off the end' of a function is just laziness, and from
a purely style point of view, I would always put an explicit return.

IMAO, putting an unnecessary return at the end of a function is just
waffling, and from a purely style point of view, I would always
eliminate such a return. It's just noise.

IMDO, can I just say that, whilst I disagree with Chris, it is clear
that the rules of C allow either approach, that neither approach is
dangerously flawed, and that we need not fall out over such minor
issues of style?

Surely.

I was provoked by bytebro's accusations of "laziness"; it's not lazy
to avoid needless work.
Calm down; a good programmer is a lazy programmer.

Tak-Shing
Apr 12 '07 #11
On Apr 12, 11:16 am, Chris Dollin <chris.dol...@hp.comwrote:
William Hughes wrote:
On Apr 12, 9:49 am, Chris Dollin <chris.dol...@hp.comwrote:
bytebro wrote:
On 12 Apr, 11:20, "quarkLore" <agarwal.prat...@gmail.comwrote:
I have seen redundant return statements at the end of function. e.g.
void init_function(void)
{
a_init();
b_init();
c_init();
return;
}
To add to the comments already made, I would say that declaring a
function of type 'void' does not mean that it doesn't return, it means
that it doesn't return a value.
IMHO, 'Dropping off the end' of a function is just laziness, and from
a purely style point of view, I would always put an explicit return.
IMAO, putting an unnecessary return at the end of a function is just
waffling, and from a purely style point of view, I would always eliminate
such a return. It's just noise.
O boy a style war!

S'not a style war: it's an attitude war.
Is too.
>
It's not just noise.

It's just noise /in that function/.

No more than the white space is "just noise /in that function/".
Neither is needed by the compiler.
>
Consider what happens if the function undergoes multiple changes.

(fx:snip)

Sometimes bad things happen, especially without unit tests and
compilers that give helpful diagnostics. I'd hesitate to ascribe
the unwritten return as the dominant cause of the problem you
describe.
Not claimed. Only that a style rule "always use explicit returns"
might be of practical use and thus an example cannot be
dismissed as "just noise".

-William Hughes
Apr 12 '07 #12

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

Similar topics

5
by: dmcconkey | last post by:
Hi folks, I've been searching for a while and haven't found my specific question anywhere else. If this has already been asked, please accept my appologies and point me to the appropriate...
4
by: zzapper | last post by:
Hi, I've inherited a mysql database with many apparently redundant tables (probably abandoned projects). Without analysing the webpages many of which are also redundant; is there any MYSQL query...
23
by: Mark Dickinson | last post by:
I have a simple 192-line Python script that begins with the line: dummy0 = 47 The script runs in less than 2.5 seconds. The variable dummy0 is never referenced again, directly or indirectly,...
4
by: Rob Conner | last post by:
No you don't need to know Zope to help me. The whole reason I'd even want to do this is because of Zope though. I made a Zope product, and now want to perfect it. some simple example code... ...
99
by: Mikhail Teterin | last post by:
Hello! Consider the following simple accessor function: typedef struct { int i; char name; } MY_TYPE; const char *
40
by: Neo The One | last post by:
I think C# is forcing us to write more code by enforcing a rule that can be summarized as 'A local variable must be assgined *explicitly* before reading its value.' If you are interested in what...
3
by: =?Utf-8?B?R3JlZw==?= | last post by:
If I have an XmlDocument with a deep hierarchy of nodes, with a documentElement having an attribute xmlns="http://someurl.com" and all children inheriting that namespace (but not having the xmlns...
5
by: Matthew Wilson | last post by:
I wrote some code to create a user and update a user on a remote box by sending emails to that remote box. When I was done, I realized that my create_user function and my update_user function were...
2
by: mde | last post by:
I'm wondering if there is a "best practice" for *creating doctests in methods* that reduces clutter/duplication of dummy instantiations. In the following code there are five (labeled 1-5) possible...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.