is a void function invocation an expression or a statement?
Wayne Throop
throopw at dg_rtp.UUCP
Thu Aug 7 06:57:43 AEST 1986
> From: aka at cbrma.UUCP (Andy Kashyap)
> A function call is an expression and can, therefore, be used anywhere an
> expression can be used. When you declare a function (void), you state that
> you intend to use that function as a statement instead, that you do not
> intend to use it in any operations. It can now only be used where a statement
> can. Keep in mind that an expression is a statement, but NOT vice-versa.
Not quite the case at all. When you declare a function void, you are
saying that its evaluation yields a void expression. You are *NOT*
saying that it is a statement. Again, let us consult Holy Writ on the
subject.
First, K&R have nothing to say about this, since void didn't exist then.
Harbison and Steele say (on page 150):
The type of the function expression must be "function returning T"
for some type T, in which case the result of the function call is of
type T. The result is not an lvalue. If T is void, however, then
the function call produces no result and may not be used in a
context that requires the call to yield a result.
That is, function invocations are *ALWAYS* expressions. Sometimes,
these expressions are void, that is, they have no value. Clear enough.
Now let's see what the ANSI C draft standard has to say (in 2.2.2):
The (nonexistent) value of a *void* *expression* (an expression that
has type void) shall not be used in any way, and explicit or
implicit conversions shall not be applied to such an expression.
(and in 3)
An *expression* is a sequence of operators and operands that
specifies how to compute a value, or (in the case of a void
expression) how to generate side effects.
Again, fairly clear. Void "thingies" (how's that for a technical term,
eh?) are expressions, and *NOT* statements. (That is, not statements
except in the sense that all expressions are statements in C, right?)
The only surprising thing here is that not all expressions return values
when they are evaluated. But this "surprise" in implicit in the meaning
of "voidness".
(As a small nit, I'd druther that the draft standard would allow a void
expression to be cast to void, but that's a fairly small quibble.)
--
There are some forms of insanity which, driven to an ultimate
expression, can become the new models of sanity.
--- Bureau of Sabotage {Frank Herbert}
--
Wayne Throop <the-known-world>!mcnc!rti-sel!dg_rtp!throopw
More information about the Comp.lang.c
mailing list