Side effect (computer science)

Side effect (computer science)

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.

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).

Temporal side effects

Side effects due to the time taken for an operation to execute are usually ignored when discussing side effects and referential transparency. In most programs it is desirable to replace a long operation with an equivalent shorter one e.g. replacing (60 / 3 * 2) with 40. There are some cases, such as with hardware timing or testing, where operation are inserted specifically for their temporal side effects e.g. Sleep(5000) or for(i=0; i < 10000; i++){}. These instructions do not change state other than taking an amount of time to complete.


Wikimedia Foundation. 2010.

Игры ⚽ Нужен реферат?

Look at other dictionaries:

  • Side effect — can mean:* Adverse drug reaction, an unintended consequence specifically arising from drug therapy * Therapeutic effect, an unintended but desirable consequence of any kind of medical treatment * Adverse effect (medicine), an unintended, and… …   Wikipedia

  • Referential transparency (computer science) — Referential transparency and referential opaqueness are properties of parts of computer programs. An expression is said to be referentially transparent if it can be replaced with its value without changing the program (in other words, yielding a… …   Wikipedia

  • Abstraction (computer science) — In computer science, abstraction is the process by which data and programs are defined with a representation similar to its pictorial meaning as rooted in the more complex realm of human life and language with their higher need of summarization… …   Wikipedia

  • Assignment (computer science) — In computer programming, an assignment statement sets or re sets the value stored in the storage location(s) denoted by a variable name. In most imperative computer programming languages, assignment statements are one of the basic statements.… …   Wikipedia

  • Optimization (computer science) — In computing, optimization is the process of modifying a system to make some aspect of it work more efficiently or use fewer resources. For instance, a computer program may be optimized so that it executes more rapidly, or is capable of operating …   Wikipedia

  • Lock (computer science) — In computer science, a lock is a synchronization mechanism for enforcing limits on access to a resource in an environment where there are many threads of execution. Locks are one way of enforcing concurrency control policies. Contents 1 Types 2… …   Wikipedia

  • Computer-supported collaboration — (CSC) research focuses on technology that affects groups, organizations, communities and societies, e.g., voice mail and text chat. It grew from cooperative work study of supporting people s work activities and working relationships. As net… …   Wikipedia

  • Science fiction film — is a film genre that uses science fiction: speculative, science based depictions of phenomena that are not necessarily accepted by mainstream science, such as extraterrestrial life forms, alien worlds, extrasensory perception, and time travel,… …   Wikipedia

  • Science in the Middle Ages — Science, and particularly geometry and astronomy, was linked directly to the divine for most medieval scholars. Since God created the universe after geometric and harmonic principles, to seek these principles was therefore to seek and worship God …   Wikipedia

  • computer — computerlike, adj. /keuhm pyooh teuhr/, n. 1. Also called processor. an electronic device designed to accept data, perform prescribed mathematical and logical operations at high speed, and display the results of these operations. Cf. analog… …   Universalium

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”