T
trubar a
Hi
I realize my question may be a bit stupid and perhaps completely
meaningless, but I’m new to programming and thus can’t be sure whether
understanding what the author really meant is essential to my
understanding of certain programming concepts
The book from which I’m learning C# has this to say about expressions
and their side effects:
“ Expressions return values, but they can also have side effects.
• A side effect is an action that affects the state of the program.
• Many expressions are evaluated only for their side effects.
You can create a statement from an expression by placing a statement
terminator (semicolon)
after it. Any value returned by the expression is discarded. For
example, the following
code shows an expression statement. It consists of the assignment
expression (an assignment
operator and two operands) followed by a semicolon. This does the
following two things:
• The expression assigns the value on the right of the operator to the
memory location referenced
by variable x. Although this is probably the main reason for the
statement, this
is considered the side effect.
• After setting the value of x, the expression returns with the new
value of x. But there is
nothing to receive this return value, so it is ignored. ”
a) If I understand correctly the above quote, then what author states
is that if we have a statement like ‘x = 10;’ then an expression is
the whole statement and not just the part on the right side of an
assignment operator?
b) If the whole statement is considered an expression, then why would
assignment be considered a side effect and not the “main effect”?
“If an assignment of value ‘10’ to variable ‘x’ is a side effect,then
what is the primary purpose ( aka main effect ) of this expression?”
c) “After setting the value of x, the expression returns with the new
value of x. But there is
nothing to receive this return value, so it is ignored.”
I assume what returns is not the whole expression ‘x = 10’, since in
that case ‘x’ would again be assigned a number? So I assume the
expression has by now been “transformed” to a single value of x ( thus
now expression no longer contains ‘=10’ )?
Thank you
I realize my question may be a bit stupid and perhaps completely
meaningless, but I’m new to programming and thus can’t be sure whether
understanding what the author really meant is essential to my
understanding of certain programming concepts
The book from which I’m learning C# has this to say about expressions
and their side effects:
“ Expressions return values, but they can also have side effects.
• A side effect is an action that affects the state of the program.
• Many expressions are evaluated only for their side effects.
You can create a statement from an expression by placing a statement
terminator (semicolon)
after it. Any value returned by the expression is discarded. For
example, the following
code shows an expression statement. It consists of the assignment
expression (an assignment
operator and two operands) followed by a semicolon. This does the
following two things:
• The expression assigns the value on the right of the operator to the
memory location referenced
by variable x. Although this is probably the main reason for the
statement, this
is considered the side effect.
• After setting the value of x, the expression returns with the new
value of x. But there is
nothing to receive this return value, so it is ignored. ”
a) If I understand correctly the above quote, then what author states
is that if we have a statement like ‘x = 10;’ then an expression is
the whole statement and not just the part on the right side of an
assignment operator?
b) If the whole statement is considered an expression, then why would
assignment be considered a side effect and not the “main effect”?
“If an assignment of value ‘10’ to variable ‘x’ is a side effect,then
what is the primary purpose ( aka main effect ) of this expression?”
c) “After setting the value of x, the expression returns with the new
value of x. But there is
nothing to receive this return value, so it is ignored.”
I assume what returns is not the whole expression ‘x = 10’, since in
that case ‘x’ would again be assigned a number? So I assume the
expression has by now been “transformed” to a single value of x ( thus
now expression no longer contains ‘=10’ )?
Thank you