All Topics  
Side effect (computer science)

 

   Email Print
   Bookmark   Link






 

Side effect (computer science)



 
 
In computer science
Computer science

Computer science is the study of the theoretical foundations of information and computation, and of practical techniques for their implementation and application in computer systems....
, a function
Subroutine

In computer science, a subroutine or subprogram is a portion of computer code within a larger computer program, which performs a specific task and is relatively independent of the remaining code....
 or expression
Expression (programming)

An expression in a programming language is a combination of value s, variables, operator s, and function s that are interpreted according to the particular Order of operations and of association for a particular programming language, which computes and then produces another value....
 is said to produce a side effect if it modifies some state
State (computer science)

In computer science and automata theory, a state is a unique configuration of information in a program or machine. It is a concept that occasionally extends into some forms of systems programming such as Lexical analysiss and parsers....
 in addition to returning a value. For example, a function might modify a global or a static variable
Static variable

In computer programming, the data value of a static variable persists for the life of the running process. Typically, a static variable has a broader Scope than other variables....
, 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.






Discussion
Ask a question about 'Side effect (computer science)'
Start a new discussion about 'Side effect (computer science)'
Answer questions from other users
Full Discussion Forum



Encyclopedia


In computer science
Computer science

Computer science is the study of the theoretical foundations of information and computation, and of practical techniques for their implementation and application in computer systems....
, a function
Subroutine

In computer science, a subroutine or subprogram is a portion of computer code within a larger computer program, which performs a specific task and is relatively independent of the remaining code....
 or expression
Expression (programming)

An expression in a programming language is a combination of value s, variables, operator s, and function s that are interpreted according to the particular Order of operations and of association for a particular programming language, which computes and then produces another value....
 is said to produce a side effect if it modifies some state
State (computer science)

In computer science and automata theory, a state is a unique configuration of information in a program or machine. It is a concept that occasionally extends into some forms of systems programming such as Lexical analysiss and parsers....
 in addition to returning a value. For example, a function might modify a global or a static variable
Static variable

In computer programming, the data value of a static variable persists for the life of the running process. Typically, a static variable has a broader Scope than other variables....
, 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.

Imperative programming
Imperative programming

In computer science, imperative programming is a programming paradigm that describes computation in terms of statement s that change a program state ....
 is known for employing side effects to make programs function. Functional programming
Functional programming

In computer science, functional programming is a programming paradigm that treats computation as the evaluation of function s and avoids program state and immutable object data....
 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
Status register

A status register is a collection of Flag bits for a Central processing unit. A popular example of a status register is the FLAGS register of x86 architecture based microprocessors....
. This causes a problem when designing a CPU that has an instruction pipeline
Instruction pipeline

File:5 Stage Pipeline.svgAn instruction pipeline is a technique used in the design of computers and other digital electronic devices to increase their instruction throughput ....
 and supports instructions with side-effects. Care must be taken to avoid this hazard
Hazard (computer architecture)

In computer architecture, a hazard is a potential problem that can happen in a Instruction pipelined central processing unit. It refers to the possibility of erroneous computation when a CPU tries to simultaneously execute multiple instructions which exhibit data dependence....
 - 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
Pure function

In computer programming, a function may be described as pure if both these statements about the function hold:# The function always evaluates the same result value given the same argument value....
 (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.