We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Referential Transparency

Pure functions are always referentially transparent.

"Referential transparency" is a fancy way of saying that a function call can be replaced by its would-be return value because it's the same every time. Referentially transparent functions can be safely memoized. For example add(2, 3) can be replaced by the value 5.

The great thing about pure functions is that it's always safe to memoize them. Impure functions often can't be memoized because they might perform a side effect in addition to returning a static value, or they might return different values given the same arguments.

Should I Always Memoize?

No! Memoization is a tradeoff between memory and speed. If your function is fast to execute, it's probably not worth memoizing, because the amount of memory your program will need to store the results will go way up.

It's also a bunch of extra code to write, so you should only do it if you have a good reason.