473,387 Members | 1,517 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,387 software developers and data experts.

repeated code before multiple return statements

Hello!
Please advise on the following issue. I have a function which returns
1 or 0 in multiple branches, and it must perform the same code before
each return, like this:
int fun()
{
if (some code)
{
restore(param1,param2,param3,param4,param5);
return 1;
}
else if (some code)
{
restore(param1,param2,param3,param4,param5);
return 0;
}
restore(param1,param2,param3,param4,param5);
return 1;
}

Is there any alternative way to execute some code upon return from the
function in pure C? Repeated code makes my function more than twice
longer and it losts its readability. The only thing coming to my mind
is goto.

Thanks, Alex.

Jul 27 '07 #1
14 1910
Alexander Stoyakin wrote:
>
Hello!
Please advise on the following issue. I have a function which returns
1 or 0 in multiple branches, and it must perform the same code before
each return, like this:
int fun()
{
if (some code)
{
restore(param1,param2,param3,param4,param5);
return 1;
}
else if (some code)
{
restore(param1,param2,param3,param4,param5);
return 0;
}
restore(param1,param2,param3,param4,param5);
return 1;
}

Is there any alternative way to execute some code upon return from the
function in pure C? Repeated code makes my function more than twice
longer and it losts its readability. The only thing coming to my mind
is goto.
What about something like this?

int fun()
{
int retval;

if ( some code )
retval = 1;
else if ( some other code )
retval = 0;
else
retval = 1;

restore(param1, param2, param3, param4, param5);

return retval;
}

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>

Jul 27 '07 #2
Alexander Stoyakin <st***@ua.fmwrites:
Please advise on the following issue. I have a function which returns
1 or 0 in multiple branches, and it must perform the same code before
each return, like this:
int fun()
{
if (some code)
{
restore(param1,param2,param3,param4,param5);
return 1;
}
else if (some code)
{
restore(param1,param2,param3,param4,param5);
return 0;
}
restore(param1,param2,param3,param4,param5);
return 1;
}
int fun2()
{
if (some code)
return 1;
else if (some code)
return 0;
else
return 1;
}

int fun()
{
int retval = fun2();
restore(param1,param2,param3,param4,param5);
return retval;
}
--
Go not to Usenet for counsel, for they will say both no and yes.
Jul 27 '07 #3
On Jul 27, 2:00 pm, Kenneth Brody <kenbr...@spamcop.netwrote:
What about something like this?

int fun()
{
int retval;

if ( some code )
retval = 1;
else if ( some other code )
retval = 0;
else
retval = 1;

restore(param1, param2, param3, param4, param5);

return retval;
}
How about

int retval = ( some_code || !some_other_code ) ? 1 : 0;

restore(param1, param2, param3, param4, param5);
return retval;

?

(Depending on what some_code and some_other_code actually are, this
may not be readable in practice.)

Jul 27 '07 #4
Alexander Stoyakin wrote:
>..I have a function which returns
1 or 0 in multiple branches, and it must perform the same code before
each return, like this:
int fun()
{
if (some code)
/* let's call this condition_1 */
{
restore(param1,param2,param3,param4,param5);
return 1;
}
else if (some code)
/* let's call this condition_2 */
{
restore(param1,param2,param3,param4,param5);
return 0;
}
restore(param1,param2,param3,param4,param5);
return 1;
}

Is there any alternative way to execute some code upon return from the
function in pure C? Repeated code makes my function more than twice
longer and it losts its readability. The only thing coming to my mind
is goto.
Assuming the parameter lists param1..5 are identical, you wrote this:

int fun()
{
int ret_val = 1;
if (condition_1)
{
}
else if (condition_2)
{
ret_val = 0;
}
restore(param1,param2,param3,param4,param5);
return ret_val
}

Assuming "condition_1" and "condition_2" do not contain function calls
or have side effects, you wrote this:

int fun()
{
int ret_val = 1;
if (condition_2)
{
ret_val = 0;
}
restore(param1,param2,param3,param4,param5);
return ret_val
}
Roberto Waltman

[ Please reply to the group,
return address is invalid ]
Jul 27 '07 #5
Alexander Stoyakin wrote:
Hello!
Please advise on the following issue. I have a function which returns
1 or 0 in multiple branches, and it must perform the same code before
each return, like this:
int fun()
{
if (some code)
{
restore(param1,param2,param3,param4,param5);
return 1;
}
else if (some code)
{
restore(param1,param2,param3,param4,param5);
return 0;
}
restore(param1,param2,param3,param4,param5);
return 1;
}

Is there any alternative way to execute some code upon return from the
function in pure C? Repeated code makes my function more than twice
longer and it losts its readability. The only thing coming to my mind
is goto.
Two possibilities, among others:

int fun()
{
int retcode;

if(exp1) retcode = 1;
else if(exp2) retcode = 0;
else retcode = 1;

restore(p1, p2, p3, p4, p5);
return retcode;
}

or

int fun()
{
restore(p1, p2, p3, p4, p5);
if (exp1) return 1;
else if (exp2) return 0;
else return 1;
}

HTH

Jul 27 '07 #6
In article <d3********************************@4ax.com>,
Roberto Waltman <us****@rwaltman.netwrote:
>Assuming the parameter lists param1..5 are identical, you wrote this:
>int fun()
{
int ret_val = 1;
if (condition_1)
{
}
else if (condition_2)
{
ret_val = 0;
}
restore(param1,param2,param3,param4,param5);
return ret_val
}
>Assuming "condition_1" and "condition_2" do not contain function calls
or have side effects, you wrote this:
>int fun()
{
int ret_val = 1;
if (condition_2)
{
ret_val = 0;
}
restore(param1,param2,param3,param4,param5);
return ret_val
}
Let condition_1 be param1 == 0
Let condition_2 be 1/param1 param2
Then condition_1 and condition_2 do not contain function calls
or have side effects, but your revised code would go directly to

if (1/param1 param2)
{
ret_val = 0;
}

which is going to fail if param1 is 0 -- the condition protected
against in condition_1 .

--
There are some ideas so wrong that only a very intelligent person
could believe in them. -- George Orwell
Jul 27 '07 #7
On Fri, 27 Jul 2007 14:00:43 -0400, Kenneth Brody wrote:
>What about something like this?

int fun()
{
int retval;
....
return retval;
}
a.k.a. SESE (Single Entry, Single Exit)
--
Roland Pibinger
"The best software is simple, elegant, and full of drama" - Grady Booch
Jul 27 '07 #8
santosh <sa*********@gmail.comwrote:
int retcode;

if(exp1) retcode = 1;
else if(exp2) retcode = 0;
else retcode = 1;
restore(p1, p2, p3, p4, p5);
Given this...
restore(p1, p2, p3, p4, p5);
if (exp1) return 1;
else if (exp2) return 0;
else return 1;
....it seems unlikely (but possible) that OP will want to restore()
anything prior to the evaluation of exp1 and exp2.

--
C. Benson Manica | I appreciate all corrections, polite or otherwise.
cbmanica(at)gmail.com |
----------------------| I do not currently read any posts posted through
sdf.lonestar.org | Google groups, due to rampant unchecked spam.
Jul 27 '07 #9
On Fri, 27 Jul 2007 18:10:07 -0000, "C. Benson Manica"
<cb******@gmail.comwrote:
>On Jul 27, 2:00 pm, Kenneth Brody <kenbr...@spamcop.netwrote:
>What about something like this?

int fun()
{
int retval;

if ( some code )
retval = 1;
else if ( some other code )
retval = 0;
else
retval = 1;

restore(param1, param2, param3, param4, param5);

return retval;
}

How about

int retval = ( some_code || !some_other_code ) ? 1 : 0;

restore(param1, param2, param3, param4, param5);
return retval;
That will work for "this", but no necessarily "something like this"
:-) I think the OPs example was intended to be illustrative, not
necessarily his exact need.

Kenneth's solution is good, and easily generalized. Both solutions
have the desirable feature of making it plain that the "some (other)
code" affects only the return value, not the processing, and
eliminating multiple returns. (I'm not always opposed to multiple
returns, but there's no excuse for them here.)
>?

(Depending on what some_code and some_other_code actually are, this
may not be readable in practice.)
--
Al Balmer
Sun City, AZ
Jul 27 '07 #10
Al Balmer <al******@att.netwrote:
On Fri, 27 Jul 2007 18:10:07 -0000, "C. Benson Manica"
<cb******@gmail.comwrote:
int retval = ( some_code || !some_other_code ) ? 1 : 0;

restore(param1, param2, param3, param4, param5);
return retval;
That will work for "this", but no necessarily "something like this"
:-) I think the OPs example was intended to be illustrative, not
necessarily his exact need.
(Depending on what some_code and some_other_code actually are, this
may not be readable in practice.)
I think I covered the possibility that it might not be what OP was
looking for, and for sure if the real code has many branches the
conditional operator should be banished from consideration. (This is
all MHO, incidentally, as I have already been reminded today that I am
not the Pope...)

--
C. Benson Manica | I appreciate all corrections, polite or otherwise.
cbmanica(at)gmail.com |
----------------------| I do not currently read any posts posted through
sdf.lonestar.org | Google groups, due to rampant unchecked spam.
Jul 27 '07 #11
On Jul 27, 1:51 pm, Alexander Stoyakin <st...@ua.fmwrote:
Hello!
Please advise on the following issue. I have a function which returns
1 or 0 in multiple branches, and it must perform the same code before
each return, like this:
int fun()
{
if (some code)
{
restore(param1,param2,param3,param4,param5);
return 1;
}
else if (some code)
{
restore(param1,param2,param3,param4,param5);
return 0;
}
restore(param1,param2,param3,param4,param5);
return 1;

}

Is there any alternative way to execute some code upon return from the
function in pure C? Repeated code makes my function more than twice
longer and it losts its readability. The only thing coming to my mind
is goto.

Thanks, Alex.
Good question. This is one situation that inspired the development (a
long time ago) of exception mechanisms. To generalize your example a
little, many functions that honor unix return code standards are
structured like this...

int fun(...)
{
...
if (<condition1>) {
<cleanup code>
return 1;
}
< normal processing 1 >

...
if (<condition2>) {
<cleanup code>
return 2;
}
...
< normal processing 2>
... yada yada ...

return 0;
}

This is really annoying when (as usual) the cleanup code needs access
to many local variables. In a block structured languate with nested
functions, you would define a cleanup function with access to those
variables. In pseudo-C,

int fun(...)
{
<local var decls>

int cleanup(int return_code)
{
<cleanup code>
return return_code;
}

...
if (<condition1>) return cleanup(1);

< normal processing 1 >

...

if (<condition2>) return cleanup(2);

...
< normal processing 2>
... yada yada ...

return 0;
}

Since C doesn't have nested functions, one way to go is "fake" them
with macros. This strategy is far from ideal, but it does have a
rationale. It avoids the problems of goto's and introducing a very
long-lived "ret_val" variable, which has maintenance problems.

int fun(...)
{
<local var decls>

#define CLEANUP_AND_RETURN(N) do { \
<cleanup code\
return (N); \
} while (0);

...
if (<condition1>) CLEANUP_AND_RETURN(1);

< normal processing 1 >

...

if (<condition2>) CLEANUP_AND_RETURN(2);

...
< normal processing 2>
... yada yada ...

return 0;

#undef CLEANUP_AND_RETURN
}

Another way to go is bottle up all the local variables needed for
cleanup in a struct so that a non-nested cleanup function can do its
work.

struct foo_vars_s { <local var decls };

int cleanup(int ret_val, struct foo_vars_s *o)
{
<cleanup code accessing variable x as o->x >
return ret_val;
}

int fun()
{
struct foo_vars_s o[1];
< local var ref x is replaced by o->x below >

...
if (<condition1>) return cleanup(1, o);

< normal processing 1 >

...

if (<condition2>) return cleanup(2, o);

...
< normal processing 2>
... yada yada ...

return 0;
}

This isn't ideal either.

Jul 28 '07 #12
On Jul 28, 12:54 pm, Gene <gene.ress...@gmail.comwrote:
>
#define CLEANUP_AND_RETURN(N) do { \
<cleanup code\
return (N); \
} while (0);
Sorry. The last semicolon above is wrong. This should have been

#define CLEANUP_AND_RETURN(N) do { \
<cleanup code\
return (N); \
} while (0)

This trick of using a do { } while(0) wrapper allows macro invocation
to work as void function calls where a proper statement is expected,
e.g. in the "then" or "else" of an "if". First saw this in gcc
sources a long time ago.

Jul 29 '07 #13
Gene <ge**********@gmail.comwrites:
On Jul 28, 12:54 pm, Gene <gene.ress...@gmail.comwrote:
>>
#define CLEANUP_AND_RETURN(N) do { \
<cleanup code\
return (N); \
} while (0);

Sorry. The last semicolon above is wrong. This should have been

#define CLEANUP_AND_RETURN(N) do { \
<cleanup code\
return (N); \
} while (0)

This trick of using a do { } while(0) wrapper allows macro invocation
to work as void function calls where a proper statement is expected,
e.g. in the "then" or "else" of an "if". First saw this in gcc
sources a long time ago.
See also question 10.4 in the comp.lang.c FAQ, <http://www.c-faq.com/>.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <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."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jul 29 '07 #14
Alexander Stoyakin wrote:
>
Please advise on the following issue. I have a function which returns
1 or 0 in multiple branches, and it must perform the same code before
each return, like this:
int fun()
{
if (some code)
{
restore(param1,param2,param3,param4,param5);
return 1;
}
else if (some code)
{
restore(param1,param2,param3,param4,param5);
return 0;
}
restore(param1,param2,param3,param4,param5);
return 1;
}

Is there any alternative way to execute some code upon return from the
function in pure C? Repeated code makes my function more than twice
longer and it losts its readability. The only thing coming to my mind
is goto.
int fun() {
int flag;

flag = 1;
if (some code) flag = 1;
else if (some other code) flag = 0;
restore(.....);
return flag;
}

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
cbfalconer at maineline dot net
--
Posted via a free Usenet account from http://www.teranews.com

Aug 1 '07 #15

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

Similar topics

1
by: Top Gun | last post by:
In order to avoid multiple trips to the database, I would like to fill several tables in a DataSet with a single call to a stored procedure that will return resultsets for the appropriate tables. ...
11
by: Shawn Milo | last post by:
I put this together yesterday, and I thought I'd share it. It works in both IE and Mozilla Firefox. I posted something similar to this months back, but it was much longer, and only worked in IE....
4
by: DG | last post by:
Hi, Can anyone advise how to execute multiple statements in a single query batch. For example- update customers set customer_name = 'Smith' where customer_name = 'Smyth'; select * from...
5
by: Oksana Yasynska | last post by:
Hi all, I'm running Postgres 7.2.1 and I need to return multiple row sets from plpgsql function. I'm new in plpgsql but according documentation and everything I could find in the mailing list...
24
by: sureshjayaram | last post by:
In some functions where i need to return multiple error codes at multiple places, I use multiple return statements. Say for ex. if (Found == 1) { if (val == -1) return error1; } else { if...
6
by: Lelle | last post by:
Hello ! how can i insert text containg code examples from a textbox into a database using SQL insert statment. i have no problem to just add text that dont contains code and script examples...
7
by: =?Utf-8?B?QVRT?= | last post by:
HOWTO Run multiple SQL statements from ASP/ADO to an Oracle 10g. Please help, I'm trying to write an ASP page to use ADO to run a long query against an Oracle 10g database, to create tables,...
0
by: Steve Holden | last post by:
Please keep this on the list. scsoce wrote: 'Fraid the Python re implementers just decided not to do it that way. regards Steve -- Steve Holden +1 571 484 6266 +1 800 494 3119...
0
by: scsoce | last post by:
MRAB wrote: Yes, you are right, but this way findall() capture only the 'top' group. What I really need to do is to capture nested and repated patterns, say, <tabletag in html contains many <tr>, ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.