--- In [email protected], "chipaug" <[EMAIL PROTECTED]> wrote:
>
> In Chapter 0 of "Accelerated C++" the authors speak of results
> and side effects. I'm having some difficulty in distinguishing
> between them.
Two notes:
1) A "result" is what you want to get. For example, if you use the
expression 4*7, you expect to get 28 as the result of this
calculation. In other words, whatever action you ask from a computer,
this action should have some result; whether you like the result is a
different question (bugs are just one thing to mention here), but
results are always a planned effect of some (computer) action.
A "side effect" differs from the "result" in a logical way:
If you have the computer create a banking account, you expect it to
set up some data in some database tables, so this is a known result.
What you surely would not expect is that some banking software will
also associate your account with some loan data, just in case you
withdraw more money from your account than you actually have. This is
(from your side) a side effect as you don't expect it.
In other words: "results" are what you expect; but you usually don't
expect "side effects".
2) A "side effect" can have unwanted, even buggy, effects in other
areas of the system. For example, when a computer CPU calculates 4*7,
some flags will have a clear state after this calculation, and (at
least for many existing CPUs) some internal flags will have a state
which cannot be relied upon. That means, if you wanted to use this
particular flag after the calculation, you first have to set it to
some definite value.
This is a "side effect" of the calculation of 4*7.
> Also, on page 6 in Chapter 0, under the heading of "Braces and
> semicolons," there is this sentence. "The expression is optional;
> omitting its results in a null statement, which has no effect." I do
> not understand this sentence.
For example, look at this sample code:
if (conditionsMet)
;
else
{ fprintf( stderr, "ERROR: condition not met! Aborting.\n");
exit( EXIT_FAILURE);
}
This means:
If that particular condition is met (e.g. some checks on some values
have been passed without any error), then do nothing; otherwise, print
an error message and terminate the program.
This "do nothing" is an empty statement: there's nothing between the
")" at the end of the "if" and the ";" ; meaning that there's an empty
statement (or empty expression) before the ";" .
Regards,
Nico