Jump to content

Referential transparency: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
m couldn't -> could not
No edit summary
Line 1: Line 1:
{{slashdotted}}


In [[computer programming]], a '''referentially transparent [[function (programming)|function]]''' is one that, given the same [[parameter]](s), it always returns the same result.
In [[computer programming]], a '''referentially transparent [[function (programming)|function]]''' is one that, given the same [[parameter]](s), it always returns the same result.



Revision as of 09:48, 21 January 2005


In computer programming, a referentially transparent function is one that, 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 is not passed as a parameter but can be altered, the results of subsequent calls to the function can differ even if the parameters are identical.

Referential transparency is important because it allows the programmer to reason about program behavior. This can help in proving correctness, finding bugs that could not be found by testing, simplifying the algorithm, assisting in modifying the code without breaking it, or optimizing the code (by means of memoization).

Some functional programming languages enforce referential transparency for all functions.