Functional programming: Difference between revisions

Content deleted Content added
Citation bot (talk | contribs)
Altered doi-broken-date. | Use this bot. Report bugs. | #UCB_CommandLine
Cousteau (talk | contribs)
m Referential transparency: rm spaces in x=x  *  10
Line 151:
Functional programs do not have assignment statements, that is, the value of a variable in a functional program never changes once defined. This eliminates any chances of side effects because any variable can be replaced with its actual value at any point of execution. So, functional programs are referentially transparent.<ref>{{cite web|last1=Hughes |first1=John |title=Why Functional Programming Matters |url=http://www.cse.chalmers.se/~rjmh/Papers/whyfp.pdf |publisher=[[Chalmers University of Technology]]}}</ref>
 
Consider [[C (programming language)|C]] assignment statement <code>x=x * 10</code>, this changes the value assigned to the variable <code>x</code>. Let us say that the initial value of <code>x</code> was <code>1</code>, then two consecutive evaluations of the variable <code>x</code> yields <code>10</code> and <code>100</code> respectively. Clearly, replacing <code>x=x * 10</code> with either <code>10</code> or <code>100</code> gives a program a different meaning, and so the expression ''is not'' referentially transparent. In fact, assignment statements are never referentially transparent.
 
Now, consider another function such as <syntaxhighlight lang="c" inline>int plusone(int x) {return x+1;}</syntaxhighlight> ''is'' transparent, as it does not implicitly change the input x and thus has no such [[side effect (computer science)|side effects]].