It means that you can replace an expression with its value without changing the behavior of the program. It’s usually used in the context of functions: If we have some function `foo`, and it turns out that calling `foo(1, 2, 3)` returns `5`, then there should be no difference between writing `int x = foo(1, 2, 3)` and `int x = 5`.
This has some implications:
* `foo` must always return the same output for the same input, so it can’t depend on any external value that changes
* `foo` must not produce any side effects, which includes performing I/O or setting external values
“Transparent” in this context means that you can act as if the reference wasn’t there, just like light acts as if transparent glass wasn’t there. You “look” straight through the reference onto the underlying value.
For this to be true, it means that you mustn’t change that value within a function. For example, if you start with x = 5, you can’t reassign it to x = 6 within the code, because the reference x **is the same as 5** by definition. This requirement is the referential transparency.
Latest Answers