Side effect (computer science)

From Wikipedia, the free encyclopedia

Jump to: navigation, search

In computer science, a function or expression is said to produce a side effect if it modifies some state in addition to returning a 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. A "Safe" operation is one that is guaranteed to be free of side-effects, i.e., it may be relied upon to leave the state of the system unchanged. Queries are the canonical safe transactions. Safe operations are also idempotent, although the reverse is not necessarily true.

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) in the status register. This causes a problem when designing a CPU that has an instruction pipeline and supports instructions with side-effects. Care 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).

Personal tools