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
Fibre optic gyroscope

... dumped 2003-03-17 with ...

This page was created in 33.8 ms