Learning path

Full curriculum

Full curriculum

Unit content

Pure functions and immutable data

A pure function produces its result only from its explicit inputs and does not change observable state outside the function.

If a pure function is called twice with the same arguments, it produces the same result both times.

Side effects

Operations such as writing a file, changing a global variable, sending a network request or mutating a shared object are side effects. Side effects are often necessary, but separating them from pure calculations makes behavior easier to reason about and test.

Immutable data

An immutable value is not changed after it is created. Instead of modifying a value in place, a program constructs a new value that represents the updated state.

old_state → update → new_state

The old state can remain valid for other references.

Referential transparency

An expression is referentially transparent when it can be replaced by its value without changing program behavior. Pure function calls have this property when their arguments are fixed.

Why this matters

Pure functions reduce hidden dependencies: understanding the result does not require knowing which unrelated code ran before. Immutable values likewise reduce the number of places from which state can unexpectedly change.

Functional programming uses these properties heavily, while still allowing effects to occur at explicit boundaries of a real program.