473,246 Members | 1,481 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,246 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 1905
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: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.