Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Ignoring the return value of a function

11 views
Skip to first unread message

Mark Hobley

unread,
Apr 15, 2010, 2:52:52 PM4/15/10
to
I have a function with a prototype as follows:

int foobar(int n)

If I am not interested in the return value of the function, is it legal to
use the function in statement context as follows?

foobar(6); /* Is this legal? */

Or, is it necessary to create a dummy variable for the return value, and
disregard it, as follows?

int tmp;
tmp = foobar(6); /* variable tmp is not used anywhere else in the code */

Mark.

--
Mark Hobley
Linux User: #370818 http://markhobley.yi.org/

Ben Pfaff

unread,
Apr 15, 2010, 3:16:46 PM4/15/10
to
markh...@hotpop.donottypethisbit.com (Mark Hobley) writes:

> I have a function with a prototype as follows:
>
> int foobar(int n)
>
> If I am not interested in the return value of the function, is it legal to
> use the function in statement context as follows?
>
> foobar(6); /* Is this legal? */

Yes.
--
"Structure padding is the use of extraneous materials to
enhance the shape of a struct and make it more attractive to
members of the opposite struct. (See also "struct silicone.")"
--Eric Sosman

Andrew Poelstra

unread,
Apr 15, 2010, 3:20:53 PM4/15/10
to
On 2010-04-15, Ben Pfaff <b...@cs.stanford.edu> wrote:
> markh...@hotpop.donottypethisbit.com (Mark Hobley) writes:
>
>> I have a function with a prototype as follows:
>>
>> int foobar(int n)
>>
>> If I am not interested in the return value of the function, is it legal to
>> use the function in statement context as follows?
>>
>> foobar(6); /* Is this legal? */
>
> Yes.

If you use lint, it might complain about it (since the
int return value is there for a reason, therefore you
should use it, goes the theory).

If you know the return value is irrelevant, you can do

(void) foobar(6);

if it makes you feel better.

--
Andrew Poelstra
http://www.wpsoftware.net/andrew

Paul N

unread,
Apr 15, 2010, 5:21:31 PM4/15/10
to
On 15 Apr, 19:52, markhob...@hotpop.donottypethisbit.com (Mark Hobley)
wrote:

> I have a function with a prototype as follows:
>
> int foobar(int n)
>
> If I am not interested in the return value of the function, is it legal to
> use the function in statement context as follows?
>
> foobar(6);          /* Is this legal? */

Yes, and you have probably already done so without realising. printf
returns an integer, which is nearly always ignored.

Ersek, Laszlo

unread,
Apr 15, 2010, 5:57:12 PM4/15/10
to
On Thu, 15 Apr 2010, Paul N wrote:

> printf returns an integer, which is nearly always ignored.

An unacceptable practice, in my opinion. I can think of two excuses:

- You are calling multiple output operations on the same stdio stream in a
row, and you finish off that sequence with a call to ferror() or a checked
fflush(). (In the ferror() case with appropriately line-buffered or
unbuffered streams, no actual output need to have happened due to the
stream operations, so ferror() may not have a chance to signal any error,
but that is no problem, if the technique is used consistently, *and* there
is a final checked fflush() or fclose() on the stream.)

- You are writing an error message to stderr on your way to exit with a
non-zero exit status.

Sometimes, if logging doesn't work, nothing should work. (The expression
"logging works" can be defined with different guarantees.) Sometimes,
looking at an incomplete log file on a full disk, it is better to know
that nothing happened than not to know what happened.

Cheers,
lacos

Richard Bos

unread,
Apr 18, 2010, 7:45:28 AM4/18/10
to
Andrew Poelstra <apoe...@localhost.localdomain> wrote:

But please don't, because it will make most people who read your code
feel worse. Instead, get a better lint - these days, your compiler
should come with one, possibly built-in.

Richard

Nick

unread,
Apr 18, 2010, 10:23:43 AM4/18/10
to
ral...@xs4all.nl (Richard Bos) writes:

There are times when I do this - when I have a function whose return
value I usually use, but sometimes don't.

For example, the "create new stack frame" function in my interpreter
returns a pointer. This pointer can be used as a parameter to an "exit
from nested frames" to return early from a function (inside a loop,
(which creates its own frame) for example). Right at the start of
running a program I call the function to create a stack frame for the
entire program. I don't use that (early termination works entirely
differently) at all, so discard it with a (void) to mark that I know
there is a return value, and I know I don't want it.
--
Online waterways route planner | http://canalplan.eu
Plan trips, see photos, check facilities | http://canalplan.org.uk

0 new messages