Side Effects to me came into existence when interviewer asked me "What will be the output of
printf("%d",i++ + ++i + i++ + ++i ..."
This goes to infinity :'( ) when i=5 and we continuously argued on the concepts, and the result was both of us were not aware of the concept of side effects and sequence point.
Any thing can have a side effect so be careful before using things especially in the world of computer science, We know a function or an expression return something when evaluated, but sometime instead of just returning a value, functions/expressions may modify some value ( global or static variable) like in above question each expression modifies the value of i, instead of just returning something, Now this function is said to have Side Effects and this is bad.
Though Side effects are essential to enable a program to interact with the outside world (people, filesystems, other computers on networks). Yes I know, its something like necessary evil. We will learn below how to use them effectively.
WikiPedia Quoted about side effects
A function or expression is said to have a side effect if, in addition to returning a value, it also modifies some state or has an observable interaction with calling functions of the outside world. For example, a function might modify a global or static variable, modify one of its arguments, raise an exception, write data to a display or file, read data, or call other side-effecting functions. In the presence of side effects, a program’s behavior depends on history; that is, the order of evaluation matters. Understanding and debugging a function with side effects requires knowledge about the context and its possible histories
In C Language printf, post/pre increment/decrement ++, -- have side effects. It doesn’t mean you shouldn’t use them but read below how to use them effectively so you don’t run into coding trouble and smashing your head against the wall, to get the output of ugly expressions like above one.
Sequence Points to the Rescue
A sequence point defines any point in a computer program’s execution at which it is guaranteed that all side effects of previous evaluations will have been performed, and no side effects from subsequent evaluations have yet been performed. They are often mentioned in reference to C and C++, because the result of some expressions can depend on the order of evaluation of their subexpressions. Adding one or more sequence points is one method of ensuring a consistent result, because this restricts the possible orders of evaluation.
Did you understood what they are trying to say ? No.
Don’t worry sequence points are not code/expressions you will need to put in your program but is a programming standard (don’t have any other word, please suggest in comments) which asks you to evaluate all the side effects of previous expression (like i++ or ++i) before continuing further execution of program. An Example will do the trick.
int apple=5,window=2,linux=7; // Yes I am bit biased. apple++; // wild Side effect appears int samsung = ++windows + apple; // Another wild side effect
linux--; // Linux comes with side effect,Oh yes
} In above code we have 3 expressions that have side effects, now the how you define sequence points ??
The first sequence point occurs above samsung statement, where apple side effect is already evaluated, second above linux. Guess the third one where it should occur.
You should always follow above style of coding with sequence points and avoid i++ + ++i kind of expressions as you can’t get sequence points here. Tell me if you can.
Wondering what was the answer to above question(at starting), The answer was its UNPREDICTABLE. YES its something from area51 you can’t guess.
DISCLAIMER : if you think this article can be improved or any other suggestions , please leave a comment.