r/functionalprogramming • • Jan 24 '23

Question Example of a function that has referential transparency but is not pure?

I've read that Functions that have referential transparency can be replaced by their output. And also that Pure functions return the same result for the same input (which makes them referentially transparent) and don't have any effect on the rest of the program.

So what is an example of a function that has referential transparency but is not pure? Does a function which only depends on its inputs but modifies a global variable still have referential transparency? It wouldn't be pure from my understanding because it modifies other parts of the program.

20 Upvotes

25 comments sorted by

View all comments

Show parent comment

4

u/WikiSummarizerBot Jan 24 '23

Referential transparency

In computer science, referential transparency and referential opacity are properties of parts of computer programs. An expression is called referentially transparent if it can be replaced with its corresponding value (and vice-versa) without changing the program's behavior. This requires that the expression be pure – its value must be the same for the same inputs and its evaluation must have no side effects. An expression that is not referentially transparent is called referentially opaque.

[ F.A.Q | Opt Out | Opt Out Of Subreddit | GitHub ] Downvote to remove | v1.5