473,738 Members | 8,397 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

return in void functions

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;
}

It works well but it is considered a good programming technic?

TIA, Max.
Jul 22 '05 #1
27 2671
Maximus wrote:
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;
}

It works well but it is considered a good programming technic?

Some will quibble about that stylistically (preferring a single exit
point), but there's nothing wrong with that technique.

[Of course, you could just as easily have written:

void SetMapLayer()
{
if (Map) layer = LAYER_MAP;
}

]

HTH,
--ag

--
Artie Gold -- Austin, Texas

Jul 22 '05 #2

"Maximus" <ma*******@vide otron.ca> wrote in message news:rp******** ************@we ber.videotron.n et...
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;
}

It works well but it is considered a good programming technic?

In structured code, you'd want to avoid willy nilly returning from inside functions,
but in this case it probably doesn't make any difference. The scary ones are
things like:

void SomeFunc() {
if(...) {
while( ... ) {
if(...) {
try {
if(...) return
...
Jul 22 '05 #3
"Maximus" <ma*******@vide otron.ca> wrote in message
news:rp******** ************@we ber.videotron.n et...
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;
}

It works well but it is considered a good programming technic?

TIA, Max.


I don't like your construction for two reasons:

1) It breaks the structured programming "rule" that each function have only
one return point. I put the word "rule" in quotes because I break it myself
anytime I feel like it. But here there is no reason to.

2) It is effectively a double negative. We set layer to LAYER_MAP if !!Map.

Don't you think this is much clearer?

void SetMapLayer()
{
if (Map)
layer = LAYER_MAP;
}

One return point, no double negatives.

Getting back to your original question, I would use return in a void
function if I thought the net effect was to make the code simpler. Example:

void fribble(int thing)
{
if (thing == 0)
return; // explanation of why here
// 15 lines of code here
}

The structured programming version is

void fribble(int thing)
{
if (thing != 0)
{
// 15 lines of code here
}
}

which I think is somewhat less readable, although by no means a serious
mistake.

--
Cy
http://home.rochester.rr.com/cyhome/
Jul 22 '05 #4
"Maximus" <ma*******@vide otron.ca> wrote in message
news:rp******** ************@we ber.videotron.n et...
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;
}

It works well but it is considered a good programming technic?


This is a matter of 'taste' or 'style'. I prefer to have a
function have only one exit point (not always possible, but
I strive for it.)

I'd write the above as:

void SetMapLayer()
{
if(Map)
layer = LAYER_MAP;
}

which effectively does the same thing.

Also, even if I'd written it as you have, I'd put the 'return'
statement on a separate line:

if( !Map )
return;

layer = LAYER_MAP;

This makes it more quickly obvious (to me at least) that
the 'linear' execution flow is interrupted. I'd probably
also add a comment for more quick visual identification:

if( !Map )
return; /* EXIT POINT */
/* I also like a blank line here */
layer = LAYER_MAP;

Especially if more code is added after the assignment, this
makes the 'return' stand out.

Again, this is all a matter of 'style'. IMO more important that
the actual 'style' you use, is that you use it consistently.

-Mike
Jul 22 '05 #5
Mike Wahler wrote:
"Maximus" <ma*******@vide otron.ca> wrote in message
news:rp******** ************@we ber.videotron.n et...
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;
}

It works well but it is considered a good programming technic?

This is a matter of 'taste' or 'style'. I prefer to have a
function have only one exit point (not always possible, but
I strive for it.)

I'd write the above as:

void SetMapLayer()
{
if(Map)
layer = LAYER_MAP;
}

which effectively does the same thing.

Also, even if I'd written it as you have, I'd put the 'return'
statement on a separate line:

if( !Map )
return;

layer = LAYER_MAP;

This makes it more quickly obvious (to me at least) that
the 'linear' execution flow is interrupted. I'd probably
also add a comment for more quick visual identification:

if( !Map )
return; /* EXIT POINT */
/* I also like a blank line here */
layer = LAYER_MAP;

Especially if more code is added after the assignment, this
makes the 'return' stand out.

Again, this is all a matter of 'style'. IMO more important that
the actual 'style' you use, is that you use it consistently.

-Mike


It's not just a matter of 'style.' Every extra point of return makes
code much harder to debug, since state-validation code to be executed
before a function returns must be duplicated for each point of return.
Take this advice from someone who has learned the error of his ways:
Keep it down to one exit point whenever possible. The only exception to
this rule should be the throwing of exceptions.

Some languages, like Tcl, do have great support for techniques relying
on multiple points of return. C++ is not such a language.

-Jeff

Jul 22 '05 #6

"Cy Edmunds" <ce******@spaml ess.rochester.r r.com> wrote in message
news:p2******** ***********@twi ster.nyroc.rr.c om...
"Maximus" <ma*******@vide otron.ca> wrote in message
news:rp******** ************@we ber.videotron.n et...
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;
}

It works well but it is considered a good programming technic?

TIA, Max.
I don't like your construction for two reasons:

1) It breaks the structured programming "rule" that each function have

only one return point. I put the word "rule" in quotes because I break it myself anytime I feel like it. But here there is no reason to.

2) It is effectively a double negative. We set layer to LAYER_MAP if !!Map.
Don't you think this is much clearer?

void SetMapLayer()
{
if (Map)
layer = LAYER_MAP;
}

One return point, no double negatives.

Getting back to your original question, I would use return in a void
function if I thought the net effect was to make the code simpler. Example:
void fribble(int thing)
{
if (thing == 0)
return; // explanation of why here
// 15 lines of code here
}

The structured programming version is

void fribble(int thing)
{
if (thing != 0)
{
// 15 lines of code here
}
}

which I think is somewhat less readable, although by no means a serious
mistake.

--
Cy
http://home.rochester.rr.com/cyhome/


ok, help me out here folks, cause I don't quite know how to reply just to
this comment, but here's mine

function which take on the form:
if( !condition ) // did caller do something stupid
{
print error
return
}
else
{
lots of code
}

are to be avoided as I've learned. The reason: runtime inefficiency due to
cpu branch prediction behavior.
If I remember correctly, the cpu will pipeline the code which errors and
exits instead of the typical function
code resulting is a few holes in the pipeline when it has to skip the error
and exit code, but worse - all of the
typical function code will have go into the pipeline "late". I might
suggest either restructing the if statement or
ensuring that your compiler fixes it for you and generates optimized object
code.

Please do correct me if I am wrong, I hate storing incorrect knowledge in my
head.

Andrew
Jul 22 '05 #7
In article <e-*************** *****@comcast.c om>,
"Andrew" <an****@wallser ver.net> wrote:
"Cy Edmunds" <ce******@spaml ess.rochester.r r.com> wrote in message
news:p2******** ***********@twi ster.nyroc.rr.c om...
"Maximus" <ma*******@vide otron.ca> wrote in message
news:rp******** ************@we ber.videotron.n et...
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;
}

It works well but it is considered a good programming technic?

TIA, Max.
I don't like your construction for two reasons:

1) It breaks the structured programming "rule" that each function have

only
one return point. I put the word "rule" in quotes because I break it

myself
anytime I feel like it. But here there is no reason to.

2) It is effectively a double negative. We set layer to LAYER_MAP if

!!Map.

Don't you think this is much clearer?

void SetMapLayer()
{
if (Map)
layer = LAYER_MAP;
}

One return point, no double negatives.

Getting back to your original question, I would use return in a void
function if I thought the net effect was to make the code simpler.

Example:

void fribble(int thing)
{
if (thing == 0)
return; // explanation of why here
// 15 lines of code here
}

The structured programming version is

void fribble(int thing)
{
if (thing != 0)
{
// 15 lines of code here
}
}

which I think is somewhat less readable, although by no means a serious
mistake.

--
Cy
http://home.rochester.rr.com/cyhome/


ok, help me out here folks, cause I don't quite know how to reply just to
this comment, but here's mine

function which take on the form:
if( !condition ) // did caller do something stupid
{
print error
return
}
else
{
lots of code
}

are to be avoided as I've learned. The reason: runtime inefficiency due to
cpu branch prediction behavior.


While that may be true on whatever platform you learned on, different
CPU's predict branches differently.But this is completely outside the
scope of the C++ language. Besides, since the compiler is free to
optimize as it sees fit, as long as it doesn't change the actual meaning
of the code, it could very well reverse the order of the "if {...}" and
"else {...}" sections of code upon seeing that the condition is "not"-ed.
If I remember correctly, the cpu will pipeline the code which errors and
exits instead of the typical function
code resulting is a few holes in the pipeline when it has to skip the error
and exit code, but worse - all of the
typical function code will have go into the pipeline "late". I might
suggest either restructing the if statement or
ensuring that your compiler fixes it for you and generates optimized object
code.

Please do correct me if I am wrong, I hate storing incorrect knowledge in my
head.


You may be right for a particular platform, but you are most
assuredly wrong for many of the platforms out there.

Jul 22 '05 #8
Hello,

Jeff Schwab <je******@comca st.net> writes:
It's not just a matter of 'style.' Every extra point of return makes
code much harder to debug, since state-validation code to be executed
before a function returns must be duplicated for each point of return.


This does not sound very severe. It is a matter of seconds to search for
every occurrence of the word "return" in a function and insert a common
line before that. I think conceptually code like

if(whatever)
return;

is very clear and clean. It just means "Do not apply the rest of the
function if condition _whatever_ is met".

Have a good 2004,
Chris Dams
Jul 22 '05 #9

"Jeff Schwab" <je******@comca st.net> skrev i en meddelelse
news:W9******** ************@co mcast.com...
Mike Wahler wrote:
"Maximus" <ma*******@vide otron.ca> wrote in message
news:rp******** ************@we ber.videotron.n et...
Hi,
I was just wondering, is it good to use return without arguments in a voidfunction as following:

void SetMapLayer()
{
if( !Map ) return;
layer = LAYER_MAP;
}

It works well but it is considered a good programming technic?

This is a matter of 'taste' or 'style'. I prefer to have a
function have only one exit point (not always possible, but
I strive for it.)

I'd write the above as:

void SetMapLayer()
{
if(Map)
layer = LAYER_MAP;
}

which effectively does the same thing.

Also, even if I'd written it as you have, I'd put the 'return'
statement on a separate line:

if( !Map )
return;

layer = LAYER_MAP;

This makes it more quickly obvious (to me at least) that
the 'linear' execution flow is interrupted. I'd probably
also add a comment for more quick visual identification:

if( !Map )
return; /* EXIT POINT */
/* I also like a blank line here */
layer = LAYER_MAP;

Especially if more code is added after the assignment, this
makes the 'return' stand out.

Again, this is all a matter of 'style'. IMO more important that
the actual 'style' you use, is that you use it consistently.

-Mike


It's not just a matter of 'style.' Every extra point of return makes
code much harder to debug, since state-validation code to be executed
before a function returns must be duplicated for each point of return.
Take this advice from someone who has learned the error of his ways:
Keep it down to one exit point whenever possible. The only exception to
this rule should be the throwing of exceptions.

Some languages, like Tcl, do have great support for techniques relying
on multiple points of return. C++ is not such a language.

-Jeff


Actually, C++ has a very nice feature for this - the deterministic execution
of destructors. This one feature is also the reason that the "one exit rule"
in C++ does not have to be taken to serious. In other languages, I would be
far more careful to have only one exit point.

Kind regards
Peter
Jul 22 '05 #10

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

Similar topics

23
1926
by: REH | last post by:
The other day I did something like this: void foo { } void bar() { return foo(); }
16
1993
by: G Patel | last post by:
Hi, If I want to call functions that don't return int without declaring them, will there be any harm? I only want to assign the function(return value) to the type that it returns, so I don't see how the return value comes to play here. Ex
15
6730
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 i = 0,j; j = sample(0);
14
3714
by: zeroDontSpamtype | last post by:
Hi, Why do strcpy and strcat (and strupr and strlwr in some nonstandard implementations) return a char*? Surely the logical (and DMA-safe) )return type for these would have been void?? Thanks, James McLaughlin.
10
1437
by: geoffblanduk_nospam | last post by:
Just curious, we no longer require void on a method that takes no parameters; void methodWithNoParameters(void) has been replaced by void methodWithNoParameters(). Is there a good reason we still have to specify void for a method that returns no result? For example; private void methodWithNoReturn() could been replaced by private
18
2451
by: skishorev | last post by:
Hi, Here I am taking two functions. void f(int,int) and another one is float f(int,int). Is it possible to overload with return values. Thx, kishore
4
6952
by: msolem | last post by:
I have some code where there are a set of functions that return pointers to each other. I'm having a bit of a hard time figuring out the correct type to use to do that. The code below works but I'm defining the functions as void*, and then casting when I use them. This code is going into a general purpose framework, and it would be much nicer if the user didn't need to do any casting. Can someone tell me how to set up those typedefs...
45
899
by: noridotjabi | last post by:
What is the purpose of the function pointer? Why do you need a pointer to a function. I cannot really think of any application where this is the only or even easiest solution to a problem. I'm sure there are really good aplications for it I just cannot think of any, so if anyone can tell me why a pointer to a function is nessisary and when it can/should be used I would apreciate that. Thanks. Nori
18
4058
by: Pedro Pinto | last post by:
Hi there once more........ Instead of showing all the code my problem is simple. I've tried to create this function: char temp(char *string){ alterString(string); return string;
0
8969
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9335
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9208
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6751
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6053
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4825
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3279
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 we have to send another system
2
2745
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2193
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.