Glossary termConcept

Pure Function

Function that always returns the same output for the same inputs and has no side effects, such as modifying outside state or performing I/O.

A pure function meets two conditions. First, its result depends only on its arguments, so calling it with the same inputs always produces the same output. Second, it causes no observable side effects: it does not modify global variables or its arguments, write to files, print to the screen, send network requests or read the clock. A function that adds two numbers is pure; one that returns the current time, generates a random number or updates a database record is not. Purity is a property of behavior rather than of syntax.

Pure functions are the foundation of functional programming, and languages such as Haskell enforce purity by tracking effects in the type system. Because a pure call can be replaced by its result without changing a program's meaning, a property called referential transparency, such functions are easier to test, reason about and reuse. Their results can be cached through memoization, computed lazily or in any order and run in parallel without the coordination that shared mutable state requires. Many codebases adopt purity as a discipline even in languages that do not enforce it.

No useful program can be entirely pure, since it must eventually read input and produce output. A common approach is to keep a pure core of logic and push effects to the edges, an architecture sometimes described as functional core, imperative shell. User interface libraries such as React encourage components that render purely from their inputs, and spreadsheet formulas behave much like pure functions. The main trade-offs are that copying data instead of modifying it can cost performance and that some algorithms are more natural to express with mutation.

Key points

  • A pure function's output depends only on its inputs, and it has no observable effect on anything outside itself.
  • Referential transparency allows a pure function call to be replaced by its result without changing program behavior.
  • Pure functions are straightforward to test, cache and run in parallel.
  • Practical programs keep a pure core of logic and isolate side effects such as I/O at the boundaries.

Related entries

Glossary term

Programming paradigm that builds software by composing pure functions, treating computation as evaluating expressions rather than changing state.

Glossary term

Mutability

Programming

The property that determines whether an object or value can be changed after it is created; immutable values stay fixed once made.

Glossary term

Memoization

Programming

Optimization technique that caches a function's results for given inputs so that repeated calls with the same arguments return instantly.

Glossary term

Function that takes other functions as arguments or returns a function as its result, such as map, filter and reduce.

Glossary term

Closure

Programming

Function bundled with references to variables from the scope where it was created, letting it keep using them after that scope has finished.

Glossary term

Idempotence

ProgrammingDevOps

Property of an operation that produces the same result whether it is applied once or many times, important for safe retries and automation.

Technology

React

Programming

Open-source JavaScript library from Meta for building user interfaces out of reusable components that update efficiently when data changes.

Glossary term

Unit Testing

Programming

Practice of verifying that small, individual pieces of code, such as single functions or classes, behave correctly when tested in isolation.

Referenced by

Other entries that discuss or point to Pure Function.