Encyclopedia > Referential transparency

  Article Content

Referential transparency

In computer programming, a function is called referentially transparent if, given the same parameter(s), it always returns the same result.

While in mathematics all functions are referentially transparent, in programming this is not always the case. For example, take a "function" that takes no parameters and returns input from the keyboard. A call to this function may be GetInput(). The return value of GetInput() depends on what the user feels like typing in, so multiple calls to GetInput() with identical parameters (the empty list) may return different results.

A more subtle example is that of a "function" that uses a global variable to help it compute its results. Since this variable isn't passed as a parameter but can be altered, the results of subsequent calls to the function can differ even if the parameters are identical.

Why is referential transparency important? Because it allows the programmer to reason about program behavior, which can help in proving correctness, finding bugs that couldn't be found by testing, simplifying the algorithm, assist in modifying the code without breaking it, or even finding ways of optimizing[?] it.

Some functional programming languages enforce referential transparency for all functions.



All Wikipedia text is available under the terms of the GNU Free Documentation License

 
  Search Encyclopedia

Search over one million articles, find something about almost anything!
 
 
  
  Featured Article
East Hampton North, New York

... is 82.46% White, 7.39% African American, 0.08% Native American, 1.62% Asian, 0.08% Pacific Islander, 5.94% from other races, and 2.43% from two or more races. 16.62% of ...

 
 
 
This page was created in 21.9 ms