Referential Transparency

Senthil Nayagan
1 min readMay 1, 2018

--

What is Referential Transparency in General?

A context in a sentence is referential transparent if replacing a term in that context by another term that refers to the same entity doesn’t alter the meaning.

Example

The Scottish Parliament meets in the capital of Scotland.

Means the same as:
The Scottish Parliament meets in Edinburgh.

Referential Transparency in Software Context

An expression is said to be referentially transparent if it can be replaced with its value without changing the behavior of a program.

In other words, yielding a program that has the same effects and output on the same input.

Pure functions are referential transparent.

Any function, f(a)=b is a pure function, if for the same “a” we end up getting the same “b” without any internal or external state change. Look at the code block below:

val x = 7
val c = x + 2

Here, the value of “c” remains 9 even if we substitute the expression with the value of x which is 7.

// Here, x's value being changed and hence it's not referentially transparent.val x = x + 2

In functional programming only referentially transparent functions are considered.

Substitution Model

The model of substituting values for expressions is called Substitution Model.

--

--

Senthil Nayagan
Senthil Nayagan

Written by Senthil Nayagan

I am a Data Engineer by profession, a Rustacean by interest, and an avid Content Creator.

No responses yet