Side effect (computer science)
Of a function, an additional effect besides returning a value
- 5 min read
- 4 chapters
Related on Lably
Through Computer science, Expression (computer science) and Local variable
Functional programming aims to minimize or eliminate side effects. The lack of side effects makes it easier to do formal verification of a program. The functional language Haskell eliminates side effects such as I/O and other stateful computations by replacing them with monadic actions. Functional languages such as Standard ML, Scheme and Scala do not restrict side effects, but it is customary for programmers to avoid them.
Effect systems extend types to keep track of effects, permitting concise notation for functions with effects, while maintaining information about the extent and nature of side effects. In particular, functions without effects correspond to pure functions.
Assembly language programmers must be aware of hidden side effects—instructions that modify parts of the processor state which are not mentioned in the instruction's mnemonic. A classic example of a hidden side effect is an arithmetic instruction that implicitly modifies condition codes (a hidden side effect) while it explicitly modifies a register (the intended effect). One potential drawback of an instruction set with hidden side effects is that, if many instructions have side effects on a single piece of state, like condition codes, then the logic required to update that state sequentially may become a performance bottleneck. The problem is particularly acute on some processors designed with pipelining (since 1990) or with out-of-order execution. Such a processor may require additional control circuitry to detect hidden side effects and stall the pipeline if the next instruction depends on the results of those effects.
Referential transparency
Absence of side effects is a necessary, but not sufficient, condition for referential transparency. Referential transparency means that an expression (such as a function) can be replaced with its value. This requires that the expression is pure, that is to say the expression must be deterministic (always give the same value for the same input) and side-effect free.
Sign in to keep reading
3 more chapters are free with a Lably account.
Temporal side effects
Side effects caused by the time taken for an operation to execute are usually ignored when discussing side effects and referential transparency. There are some cases, such as with hardware timing or testing, where operations are inserted specifically for their temporal side effects e.g. sleep(5000) or for (int i = 0; i < 10000; ++i) {}. These instructions do not change state other than taking an amount of time to complete.
Idempotence
A subroutine with side effects is idempotent if multiple calls of the subroutine have the same effect on the system state as a single call, in other words if the function from the system state space to itself associated with the subroutine is idempotent in the mathematical sense. For instance, consider the following Python program:
<span id="mwVQ"></span><span class="n" id="mwVg">x</span> <span class="o" id="mwVw">=</span> <span class="mi" id="mwWA">0</span> <span class="k" id="mwWQ">def</span><span class="w" id="mwWg"> </span><span class="nf" id="mwWw">setx</span><span class="p" id="mwXA">(</span><span class="n" id="mwXQ">n</span><span class="p" id="mwXg">):</span> <span class="k" id="mwXw">global</span> <span class="n" id="mwYA">x</span> <span class="n" id="mwYQ">x</span> <span class="o" id="mwYg">=</span> <span class="n" id="mwYw">n</span> <span class="n" id="mwZA">setx</span><span class="p" id="mwZQ">(</span><span class="mi" id="mwZg">3</span><span class="p" id="mwZw">)</span> <span class="k" id="mwaA">assert</span> <span class="n" id="mwaQ">x</span> <span class="o" id="mwag">==</span> <span class="mi" id="mwaw">3</span> <span class="n" id="mwbA">setx</span><span class="p" id="mwbQ">(</span><span class="mi" id="mwbg">3</span><span class="p" id="mwbw">)</span> <span class="k" id="mwcA">assert</span> <span class="n" id="mwcQ">x</span> <span class="o" id="mwcg">==</span> <span class="mi" id="mwcw">3</span>
setx is idempotent because the second call of setx to 3 has the same effect on the system state as the first call: x was already set to 3 after the first call, and it is still set to 3 after the second call.
A pure function is idempotent if it is idempotent in the mathematical sense. For instance, consider the following Python program:
<span id="mwfQ"></span><span class="k" id="mwfg">def</span><span class="w" id="mwfw"> </span><span class="nf" id="mwgA">abs</span><span class="p" id="mwgQ">(</span><span class="n" id="mwgg">n</span><span class="p" id="mwgw">):</span> <span class="k" id="mwhA">return</span> <span class="o" id="mwhQ">-</span><span class="n" id="mwhg">n</span> <span class="k" id="mwhw">if</span> <span class="n" id="mwiA">n</span> <span class="o" id="mwiQ"><</span> <span class="mi" id="mwig">0</span> <span class="k" id="mwiw">else</span> <span class="n" id="mwjA">n</span> <span class="k" id="mwjQ">assert</span> <span class="nb" id="mwjg">abs</span><span class="p" id="mwjw">(</span><span class="nb" id="mwkA">abs</span><span class="p" id="mwkQ">(</span><span class="o" id="mwkg">-</span><span class="mi" id="mwkw">3</span><span class="p" id="mwlA">))</span> <span class="o" id="mwlQ">==</span> <span class="nb" id="mwlg">abs</span><span class="p" id="mwlw">(</span><span class="o" id="mwmA">-</span><span class="mi" id="mwmQ">3</span><span class="p" id="mwmg">)</span>
abs is idempotent because the second call of abs to the return value of the first call to -3 returns the same value as the first call to -3.
Example
One common demonstration of side effect behavior is that of the assignment operator in C. The assignment a = b is an expression that evaluates to the same value as the expression b, with the side effect of storing the R-value of b into the L-value of a. This allows multiple assignment:
<span id="mwrA"></span><span class="n" id="mwrQ">a</span><span class="w" id="mwrg"> </span><span class="o" id="mwrw">=</span><span class="w" id="mwsA"> </span><span class="p" id="mwsQ">(</span><span class="n" id="mwsg">b</span><span class="w" id="mwsw"> </span><span class="o" id="mwtA">=</span><span class="w" id="mwtQ"> </span><span class="mi" id="mwtg">3</span><span class="p" id="mwtw">);</span><span class="w" id="mwuA"> </span><span class="c1" id="mwuQ">// b = 3 evaluates to 3, which then gets assigned to a</span>
Because the operator right associates, this is equivalent to
<span id="mwvg"></span><span class="n" id="mwvw">a</span><span class="w" id="mwwA"> </span><span class="o" id="mwwQ">=</span><span class="w" id="mwwg"> </span><span class="n" id="mwww">b</span><span class="w" id="mwxA"> </span><span class="o" id="mwxQ">=</span><span class="w" id="mwxg"> </span><span class="mi" id="mwxw">3</span><span class="p" id="mwyA">;</span>
This presents a potential hangup for novice programmers who may confuse
<span id="mwzA"></span><span class="k" id="mwzQ">while</span><span class="w" id="mwzg"> </span><span class="p" id="mwzw">(</span><span class="n" id="mw0A">b</span><span class="w" id="mw0Q"> </span><span class="o" id="mw0g">==</span><span class="w" id="mw0w"> </span><span class="mi" id="mw1A">3</span><span class="p" id="mw1Q">)</span><span class="w" id="mw1g"> </span><span class="p" id="mw1w">{}</span><span class="w" id="mw2A"> </span><span class="c1" id="mw2Q">// tests if b evaluates to 3</span>with
<span id="mw3Q"></span><span class="k" id="mw3g">while</span><span class="w" id="mw3w"> </span><span class="p" id="mw4A">(</span><span class="n" id="mw4Q">b</span><span class="w" id="mw4g"> </span><span class="o" id="mw4w">=</span><span class="w" id="mw5A"> </span><span class="mi" id="mw5Q">3</span><span class="p" id="mw5g">)</span><span class="w" id="mw5w"> </span><span class="p" id="mw6A">{}</span><span class="w" id="mw6Q"> </span><span class="c1" id="mw6g">// b = 3 evaluates to 3, which then casts to true so the loop is infinite</span>
