Pure Function
A function whose output depends only on its input and which causes no observable effect outside itself.
Pure Function describes a function with two properties: given the same input, it always returns the same output, and calling it causes no observable side effect — no mutation of shared state, no I/O, nothing the caller has to know happened besides the return value. Math.sqrt is pure; console.log is not, and neither is any function that calls it internally, because impurity is contagious upward through the call graph.
The property this buys is referential transparency: any call to a pure function can be replaced by its result without changing the program's behavior. That license is what makes Memoization safe (caching only works if the same input always deserves the same cached output), what makes reordering or parallelizing calls safe (there's no hidden ordering dependency on side effects), and what makes a Unit Test for the function complete — there's no ambient state to set up or tear down, only inputs and an expected output.
Purity is a spectrum in practice, not a binary most real programs commit to fully. A function can be "locally pure" — mutating a value it allocated itself and returns, never touching anything the caller owns — and still get most of the reasoning benefit, which is the compromise most functional-leaning code in imperative languages actually makes. Haskell pushes purity furthest, using the type system (the IO type) to statically wall off anything impure so a function's signature discloses whether it can misbehave.
The common failure mode is a function that looks pure — takes arguments, returns a value — but secretly reads a mutable global, the system clock, or random state. It passes a casual read but breaks every downstream assumption that depends on purity, usually surfacing as a test that passes alone and fails in a suite, or a cache that serves stale results.
See also6
Immutability
A value that cannot change after construction, so sharing it never risks a caller seeing another caller's edit.
Computation & Algorithms7 connections
Memoization
Caching a function's result against its input so a repeat call with the same input skips recomputation.
Computation & Algorithms6 connections
Determinism
The property that identical inputs produce identical outputs, and the foundation of caching and verification.
Systems & Tooling56 connections
Higher-Order Function
A function that takes another function as an argument, returns one, or both.
Computation & Algorithms4 connections
Closure
A function bundled with the variables from its enclosing scope at the point it was created.
Computation & Algorithms7 connections
Flaky Test
A test that passes and fails on identical input, and the most corrosive thing a suite can contain.
Testing & Verification17 connections
Linked from6
- ClosureComputation & Algorithms
A function bundled with the variables from its enclosing scope at the point it was created.
- CurryingComputation & Algorithms
Transforming a function of several arguments into a chain of one-argument functions, each returning the next.
- Higher-Order FunctionComputation & Algorithms
A function that takes another function as an argument, returns one, or both.
- ImmutabilityComputation & Algorithms
A value that cannot change after construction, so sharing it never risks a caller seeing another caller's edit.
- Lazy EvaluationComputation & Algorithms
Deferring a computation until its result is actually needed, and then caching it so it never runs twice.
- MemoizationComputation & Algorithms
Caching a function's result against its input so a repeat call with the same input skips recomputation.