Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Referential transparency, a term commonly used in functional programming, means that given a function and an input value, you will always receive the same output. That is to say there is no external state used in the function.</p> <p>Here is an example of a referential transparent function:</p> <pre><code>int plusOne(int x) { return x+1; } </code></pre> <p>With a referential transparent function, given an input and a function, you could replace it with a value instead of calling the function. So instead of calling plusOne with a parameter of 5, we could just replace that with 6. </p> <p>Another good example is mathematics in general. In mathematics given a function and an input value, it will always map to the same output value. f(x) = x + 1. Therefore functions in mathematics are referentially transparent. </p> <p>This concept is important to researchers because it means that when you have a referentially transparent function, it lends itself to easy automatic parallelization and caching. </p> <p>Referential transparency is used always in functional languages like Haskell.</p> <p>--</p> <p>In contrast there is the concept of referential opaqueness. This means the opposite. Calling the function may not always produce the same output. </p> <pre><code>//global G int G = 10; int plusG(int x) {//G can be modified externally returning different values. return x + G; } </code></pre> <p>Another example, is a member function in an object oriented programming language. Member functions commonly operate on its member variables and therefore would be referential opaque. Member functions though can of course be referentially transparent.</p> <p>Yet another example is a function that reads from a text file and prints the output. This external text file could change at any time so the function would be referentially opaque. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload