Return value of an assignment statement?
"Martin v. Löwis"
martin at v.loewis.de
Thu Feb 21 17:34:51 EST 2008
> Hi all. In C, an assignment statement returns the value assigned.
No. C doesn't have an assignment statement. Instead, in C, assignment
is an expression (just like a binary operation or a function call);
that expression evaluates to the value assigned (i.e. the result is
the value, the assignment is just a side-effect).
What you consider the assignment statement is actually an expression
statement, of the syntax
<expression> <semicolon>
So
x = y;
f();
3+4;
are all the same kind of statement.
> In python, as far as I can tell, assignment statements don't return
> anything:
Right - that's because they are statements. No statement "returns"
a value - except for the return statement, of course, but it doesn't
return it in the sense that you could write
foo = return 44
Because of the confusing meaning of "return", I find it better to
say that expressions evaluate to a value, not that they return
a value.
> The above example generates a SyntaxError.
>
> Is this correct? I just want to make sure I've understood the
> semantics.
Please try to study more on the difference between expressions
and statements.
Regards,
Martin
P.S. Just to confuse matters: GNU C also has statement expressions,
of the form
({ int y = foo (); int z;
if (y > 0) z = y;
else z = - y;
z; })
These are expressions, but allow the expressiveness of statements
(including variable declarations)
More information about the Python-list
mailing list