Chinaunix首页 | 论坛 | 博客
  • 博客访问: 66805
  • 博文数量: 14
  • 博客积分: 1410
  • 博客等级: 上尉
  • 技术积分: 170
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-20 10:58
文章分类
文章存档

2010年(5)

2009年(9)

我的朋友

分类: 嵌入式

2009-12-07 09:49:02

Side effect (computer science)

From Wikipedia, the free encyclopedia

Jump to: ,

In , a or is said to have a side effect if, in addition to producing a value, it also modifies some or has an observable interaction with calling functions or the outside world. For example, a function might modify a global or a , modify one of its arguments, raise an exception, write data to a display or file, read data, call other side-effecting functions, or launch missiles. In the presence of side effects, a program's behavior depends on history; that is, the order of evaluation matters. Because understanding an effectful program requires thinking about all possible histories, side effects often make a program harder to understand.

Side effects are essential to enable a program to interact with the outside world (people, filesystems, other computers on networks). But the degree to which side effects are used depends on the programming paradigm. is known for uncontrolled, promiscuous use of side effects. In , side effects are rarely used. Functional languages such as and do not restrict side effects, but it is customary for programmers to avoid them. The functional language restricts side effects with a static ; it uses the concept of to do stateful and IO computations.

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 which explicitly modifies a register (an overt effect) and implicitly modifies condition codes (a hidden side effect). One defect of an instruction set with many hidden side effects is that if many instructions all 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 processors designed with (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 necessary but not sufficient for referential transparency. Referential transparency means that an expression (such as a function call) can be replaced with its value; this requires that the expression has no side effects and is (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 operations 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.

[] References

  1. Haskell 98 report, .
  2. Imperative Functional Programming, Simon Peyton Jones and Phil Wadler, Conference Record of the 20th Annual ACM Symposium on Principles of Programming Languages, pages 71–84, 1993
阅读(1982) | 评论(0) | 转发(0) |
0

上一篇:I/O端口和I/O内存

下一篇:SPI驱动程序

给主人留下些什么吧!~~