Subject: Re: ignoring return values from functions.
To: None <tech-userlevel@netbsd.org (NetBSD Userlevel Technical Discussion\>
From: Robert Elz <kre@munnari.OZ.AU>
List: tech-userlevel
Date: 09/20/2001 14:39:01
    Date:        Wed, 19 Sep 2001 18:13:23 -0400 (EDT)
    From:        woods@weird.com (Greg A. Woods)
    Message-ID:  <20010919221323.5E5CDEA@proven.weird.com>

  | It is ALWAYS best to explicitly cast function return values to (void) if
  | they are expliicltly to be ignored.

If that were true, then I expect that you also write

	(void)(i++);

when you want to increment i (or "(void)++i;") as the return value
(value of the expression) there is being ignored too.

The same for any other expression that has a value that isn't
being used - which is every simple C statement, so I assume you're
also writing

	(void)(i = 0);

to make it clear that you really didn't want to use the value of the
assignment statement.

  | We're not talking about the basic
  | correctness of the code here, but rather the readability and future
  | maintainability of the code.

Throwing extra (void) around doesn't help readability or maintainability
in any way at all - just the opposite, it hides the code that actually does
something useful in the middle of lots of syntax noise.

No-one can possibly be accidentally misled into thinking that

	printf(fmt, arg, otherarg);

is somehow having its return value used - for most functions like that
which are defined to return values that are occasionally useful to have
it isn't even important to know that they return a value.  It is manifestly
obvious that the return value is being ignored, adding "(void)" doesn't
make it any more obvious - all it does is say "I know what I am doing is
right" .. though in many cases what it really means is "I added this and
it made gcc/lint shut up about my code, so this has to be right".

If a function can usefully return something that might occasionally
be useful, and that is cheap, then it should do so - like strcpy() etc
do, perhaps 90% of users of strcpy() don't want the value, but having it
there is useful sometimes, so it certainly should remain.

If lint could detect that a function has no side effects, and that its
value was being ignored - that would be a useful warning to print, but
warning about convenience return values being ignored is simply broken.

  | If all callers to a function ignore its return value then it should be
  | declared 'void' in the first place.

If writing a function for use only in a program that is never going to
use the value, then yes, I agree.

But if you're writing the function in a way that it might be useful for
purposes beyond the current program, then no way, if there's a useful
value to return, return it.   This obviously includes all library functions.

  | K&R's style isn't entirely best for production use -- it is purposefully
  | simplified to make learning the language easier.

No it isn't, it is exactly the style that K&R actually used (especially
that in the first edition) for real production code.  It works just fine.
Your quote from K&P in the later message has no relevance to anything at
all that I could see.

kre