two exiting points in void function?

L

L. Westmeier

Is there a way to have to exiting point in a void function? I don't want
to exit the program but just this function.

Any answers appreciated.

L. Westmeier
 
A

Andreas Kahari

Is there a way to have to exiting point in a void function? I don't want
to exit the program but just this function.

Any answers appreciated.


#include <stdio.h>

void foo(int e)
{
if (e == 0) {
return;
}

printf("didn't exit early, e == %d\n", e);
}
 
E

Ed Morton

L. Westmeier said:
thanks for the fast reply :)

Its good practice to only have one return point from a function. Since
you didn't know about "return;", I'm guessing you're new to C so you may
want to post a small, compilable code sample to get feedback on whether
or not you're approaching this the right way.

Ed.
 
G

Goran Larsson

Ed Morton said:
Its good practice to only have one return point from a function.

Its bad practice to only have one return point from a function as the
single return far too often results in a plurality of status flags and
nested if-statements, all needed to go from the wanted return point to
the mandated return point.
 
A

Alan Balmer

Its bad practice to only have one return point from a function as the
single return far too often results in a plurality of status flags and
nested if-statements, all needed to go from the wanted return point to
the mandated return point.

"Go To Considered Helpful"
 
G

Goran Larsson

Alan Balmer said:
"Go To Considered Helpful"

Rules demanding "no goto" are often found where rules demanding
"only one return point" are found. These rules leads to the plurality
of status flags and nested if-statements, often combined with a
nice selection of bugs.
 
K

Kevin D. Quitt

#include <stdio.h>

void foo(int e)
{
if (e == 0) {
return;
}

printf("didn't exit early, e == %d\n", e);
}

#include <stdio.h>

void foo(int e)
{
if (e != 0)
printf("didn't exit early, e == %d\n", e);
}

Same result, single return point.
 
A

Andreas Kahari

[cut]
void foo(int e)
{
if (e != 0)
printf("didn't exit early, e == %d\n", e);
}

Same result, single return point.


Not very useful for demonstrating the possibility of multiple
exit points though...
 
E

Ed Morton

Goran said:
Its bad practice to only have one return point from a function as the
single return far too often results in a plurality of status flags and
nested if-statements, all needed to go from the wanted return point to
the mandated return point.

If you need multiple returns or many status flags or multiply-nested
"ifs" to make your function readable, then it's probably too big or just
badly designed. The alternative is that it's necessarily complex in
which case having a status flag would improve readbility. e.g.

for (i = 0; i < MAX; i++) { /* False assumption from the next guy
* to work on this code: Ah, we only
* leave this loop when i reaches "MAX"
*/
....
if (foo(i) > 27) {
return 1; /* Question from the next guy to work on
* this code: Huh? Why are we returning here?
*/
}
}
/* do success leg stuff */
return 0;

vs

rslt = SUCCESS;
ret = 1;
for (i = 0; (i < MAX) && (rslt == SUCCESS); i++) {
....
if (foo(i) > 27) {
rslt = OUTOFRESOURCES;
}
}
if (rslt == SUCCESS) {
/* do success leg stuff */
ret = 0;
}
return ret;

Not only is the second version easier for the next guy to understand,
it's easier to debug if you want to, say, stick in a printf() to dump
the return code just before the function returns since now you do it in
one place instead of many and you have a variable "ret" to dump.

Disclaimer - the above is for necessarily complex functions, not for
every function, and we're discussing whether or not to use multiple
returns, not whether the above structure using a status flag is better
or worse than the equivalent with "goto"s.

Regards,

Ed.
 
S

Sheldon Simms

Its good practice to only have one return point from a function.

There are good reasons for a function to have multiple exit points
at times. For example, when a function may return an error value I
find it often helpful to do the required checking at the top of the
function and return immediately if problems are encountered.

The main logic of the function then follows unindented, not nested
in if-statements, and generally not cluttered up by braces and
extraneous syntax.

This sort of approach can also simplify the logic of the function
and improve its efficiency at the same time by recognizing and
handling special cases early.
 
D

Dan Pop

Its good practice to only have one return point from a function.

Especially if you're writing Pascal code in C. Don't forget

#define BEGIN {
#define END }

Dan
 
M

Mac

Its good practice to only have one return point from a function. Since
you didn't know about "return;", I'm guessing you're new to C so you may
want to post a small, compilable code sample to get feedback on whether
or not you're approaching this the right way.

Ed.


This sounds more like an effort at indoctrination than helpful C advice.
;-)

I find that it is perfectly reasonable to manage flow-control with early
returns, especially in short functions.

mac
--
 
E

Ed Morton

This sounds more like an effort at indoctrination than helpful C advice.
;-)

Yeah, you got me. My next followup was going to be to ask the OP to move
into my commune, renounce his family and sign over all his worldy
posessions. I'll have to be less obvious next time...
I find that it is perfectly reasonable to manage flow-control with early
returns, especially in short functions.

Well, it's not the worst thing you could do in a C program. Short,
simple functions aren't really the problem, though - it's the 100+ line
ones with returns in the middle of loops, etc. that are hard for the
next person to understand and debug.

Ed.
 
G

Goran Larsson

Ed Morton said:
Well, it's not the worst thing you could do in a C program. Short,
simple functions aren't really the problem, though - it's the 100+ line
ones with returns in the middle of loops, etc. that are hard for the
next person to understand and debug.

How is having a plurality of status variables and a plurality of nested
if-statements making it easier to understand and debug? It is impossible
to force the creation of good code by setting up some stupid rules like:
no goto
no multiple returns
no continue
no comments
no global variables
 
E

Ed Morton

Goran said:
How is having a plurality of status variables and a plurality of nested
if-statements making it easier to understand and debug?

You don't want or need either of those.

It is impossible
to force the creation of good code by setting up some stupid rules like:
no goto
no multiple returns
no continue
no comments
no global variables

That's true. You often need comments and sometimes global variables.
There are no rules that can force the creation of good code.

Ed.
 
M

Mark McIntyre

Rules demanding "no goto" are often found where rules demanding
"only one return point" are found. These rules leads to the plurality
of status flags and nested if-statements, often combined with a
nice selection of bugs.

<style war alert>
and code with multiple returns is littered with bugs too, unfreed
memory, static data not cleared, etc etc
</>

Yeah, whatever. Both models suck. Learn to be a better programmer,
design properly in the first place, and don't make mistakes.
Or Use Haskell.
 
M

Minti

L. Westmeier said:
Is there a way to have to exiting point in a void function? I don't want
to exit the program but just this function.

Any answers appreciated.

Just for a moment I moment read exiting as exciting. Nothing exiting
about it but just wanted to share.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,104
Messages
2,570,643
Members
47,247
Latest member
youngcoin

Latest Threads

Top