Side effect (computer science)

From Wikipedia, the free encyclopedia

In computer science, a function or expression is said to produce a side effect if it modifies some state in addition to its return value. For example, a function might modify a global or a static variable, modify one of its arguments, write data to a display or file, or read some data from other side-effecting functions. Side effects often make a program's behavior more difficult to predict.

Imperative programming is known for employing side effects to make programs function. Functional programming in turn is known for its minimization of side effects.

In CPU design, instruction side effects are those instructions which modify internal CPU values without explicitly stating it - for instance, generally the instruction ADD may or may not modify condition variables (carry, zero, overflow, etc). This causes a problem when designing a CPU that has an instruction pipeline and supports instructions with side-effects. Care in this case must be taken to avoid this hazard - it is possible to avoid by limiting the instruction set to instructions without side effects or in the worst case having additional control circuitry to detect the side effects and stall the pipeline if the next instruction depends on the values.

[edit] Referential transparency

Being side-effect free is necessary but not sufficient for referential transparency. Referential transparency means that an expression (such as a function call) can be replaced with the value; this requires that the expression has no side effects and is pure (always returns the same results on the same input).