473,405 Members | 2,187 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,405 software developers and data experts.

two exiting points in void function?

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

Nov 13 '05 #1
22 5939
In article <bo**********@hahn.informatik.hu-berlin.de>, L. Westmeier wrote:
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);
}
--
Andreas Kähäri
Nov 13 '05 #2
L. Westmeier wrote:
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.


Use return;

Nov 13 '05 #3
Grumble wrote:
L. Westmeier wrote:
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.

Use return;

thanks for the fast reply :-)

Nov 13 '05 #4


L. Westmeier wrote:
Grumble wrote:
L. Westmeier wrote:
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.


Use return;

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.

Nov 13 '05 #5
In article <bp********@netnews.proxy.lucent.com>,
Ed Morton <mo****************@lucent.com> wrote:
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.

--
Göran Larsson http://www.mitt-eget.com/
Nov 13 '05 #6
On Thu, 13 Nov 2003 15:21:34 GMT, ho*@invalid.invalid (Goran Larsson)
wrote:
In article <bp********@netnews.proxy.lucent.com>,
Ed Morton <mo****************@lucent.com> wrote:
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.


"Go To Considered Helpful"

--
Al Balmer
Balmer Consulting
re************************@att.net
Nov 13 '05 #7
In article <h1********************************@4ax.com>,
Alan Balmer <al******@spamcop.net> wrote:
"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.

--
Göran Larsson http://www.mitt-eget.com/
Nov 13 '05 #8
On Thu, 13 Nov 2003 12:30:48 +0000 (UTC), Andreas Kahari
<ak*******@freeshell.org> wrote:

In article <bo**********@hahn.informatik.hu-berlin.de>, L. Westmeier wrote:
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);
}


#include <stdio.h>

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

Same result, single return point.
--
#include <standard.disclaimer>
_
Kevin D Quitt USA 91387-4454 96.37% of all statistics are made up
Per the FCA, this address may not be added to any commercial mail list
Nov 13 '05 #9
In article <1e********************************@4ax.com>, Kevin D Quitt wrote:
In article <bo**********@hahn.informatik.hu-berlin.de>, L. Westmeier wrote:
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.
[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...

--
Andreas Kähäri
Nov 13 '05 #10
In <bp**********@hahn.informatik.hu-berlin.de> "L. Westmeier" <we*******@informatik.hu-berlin.de> writes:
Grumble wrote:
L. Westmeier wrote:
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.

Use return;

thanks for the fast reply :-)


Consulting your C book would have provided an even faster answer...

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #11


Goran Larsson wrote:
In article <bp********@netnews.proxy.lucent.com>,
Ed Morton <mo****************@lucent.com> wrote:

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.


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.

Nov 13 '05 #12
On Thu, 13 Nov 2003 08:51:38 -0600, Ed Morton wrote:
L. Westmeier wrote:
Grumble wrote:
L. Westmeier wrote:

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.

Use return;

thanks for the fast reply :-)


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.
Nov 13 '05 #13
In <bp********@netnews.proxy.lucent.com> Ed Morton <mo****************@lucent.com> writes:


L. Westmeier wrote:
Grumble wrote:
L. Westmeier wrote:

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.

Use return;

thanks for the fast reply :-)


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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #14
Mac
On Thu, 13 Nov 2003 08:51:38 +0000, Ed Morton wrote:


L. Westmeier wrote:
Grumble wrote:
L. Westmeier wrote:

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.

Use return;

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.

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

Nov 13 '05 #15


Mac wrote:
On Thu, 13 Nov 2003 08:51:38 +0000, Ed Morton wrote: <snip> 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.

Nov 13 '05 #16
In article <bp********@netnews.proxy.lucent.com>,
Ed Morton <mo****************@lucent.com> wrote:
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

--
Göran Larsson http://www.mitt-eget.com/
Nov 13 '05 #17


Goran Larsson wrote:
In article <bp********@netnews.proxy.lucent.com>,
Ed Morton <mo****************@lucent.com> wrote:

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?


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.

Nov 13 '05 #18
On Thu, 13 Nov 2003 16:09:27 GMT, in comp.lang.c , ho*@invalid.invalid
(Goran Larsson) wrote:
In article <h1********************************@4ax.com>,
Alan Balmer <al******@spamcop.net> wrote:
"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.


<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.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
Nov 13 '05 #19
"L. Westmeier" <we*******@informatik.hu-berlin.de> wrote in message news:<bo**********@hahn.informatik.hu-berlin.de>...
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.

--
SCO a fia'SCO'? nopes it is a fuck'SCO'
Imanpreet Singh Arora
isingh AT acm DOT org
Nov 13 '05 #20
Goran Larsson wrote:
.... snip ...
[snip] having a plurality of status variables and a plurality of
nested if-statements making it easier to understand and debug?
... snip ...


We don't need no steenkin software patents in here :-)

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 13 '05 #21
Minti <mi************@yahoo.com> spoke thus:
Just for a moment I moment read exiting as exciting. Nothing exiting
about it but just wanted to share.


Not exciting?? I thought C was always full of thrills and chills and
action-packed excitement!! ;(

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 13 '05 #22
Christopher Benson-Manica <at***@nospam.cyberspace.org> wrote in message news:<bp**********@chessie.cirr.com>...
Minti <mi************@yahoo.com> spoke thus:
Just for a moment I moment read exiting as exciting. Nothing exiting
about it but just wanted to share.


Not exciting?? I thought C was always full of thrills and chills and
action-packed excitement!! ;(


But I am not sure how many post have you come across where in people
ask for

"Is there a way to have to exCiting point in a void function?"

Though it would be quite exCiting if everybody asks questions in terms
of excitement rather than

What is the reason for not using void main()?

If one could write

What is the exciting, chilly and erotic reason for not using void
main()?

--
Imanpreet Singh Arora
isingh AT acm DOT org
Nov 13 '05 #23

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

Similar topics

6
by: James.D | last post by:
Hi, I met such a problem: //--------------------- // .h class CA { protected: void (CA::*)m_pfn(); public:
5
by: Tim Clacy | last post by:
When exiting function scope, which occurs first: a) destruction of local objects b) copy of value for return From disassembly of a debug target, it looks like the return value is copied before...
7
by: akarl | last post by:
Hi all, Why do I get a warning from gcc with the following program? $ cat test.c #include <stdio.h> int f(int n) { return n;
1
by: Karl O. Pinc | last post by:
FYI, mostly. But I do have questions as to how to write code that will continue to work in subsequent postgresql versions. See code below. begintest() uses EXIT to exit a BEGIN block from...
23
by: Boltar | last post by:
Hi I'm writing a threading class using posix threads on unix with each thread being run by an object instance. One thing I'm not sure about is , if I do the following: myclass::~myclass() {...
7
by: Pietro Cerutti | last post by:
Hi group. I have a problem in a program structured like this: void function_1(my_data_t *data); my_data_t *create_data(void); void library_function(void); int main(void) { my_data_t *data;
6
by: jacek.dziedzic | last post by:
Hello! I have some legacy C code which expects a pointer to a function to be evaluated at one point. Because of the pointer-to-function vs. pointer-to-member incompatibility, this needs to be a...
4
by: Spiros Bousbouras | last post by:
Assume you have a library which contains the functions foo1, ... , foon which the outside world knows about and may call and the functions bar1, ... , barm which the outside world doesn't know...
1
by: tiffrobe | last post by:
I'm a little lost on my program. Everything works fine except function 3. It gives out garbage numbers. Its suppose to give the distance between two points and then the area of 2 circles. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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.