Pure Function
A pure function is a function whose output depends only on its input arguments and which produces no observable side effects, such as mutating external state, performing I/O, or relying on mutable global variables. Calling a pure function…
Definition
A pure function is a function whose output depends only on its input arguments and which produces no observable side effects, such as mutating external state, performing I/O, or relying on mutable global variables. Calling a pure function with the same inputs always returns the same output.
Overview
Purity is a core concept borrowed from functional programming and mathematics, where a function is understood as a mapping from inputs to outputs with no hidden dependencies or effects. A function is pure if it satisfies two properties: referential transparency (the same arguments always produce the same result, so a call can be replaced by its return value without changing program behavior) and the absence of side effects (it does not write to a database, mutate an object passed by reference, log to the console, make a network request, or read from a clock, random number generator, or other external source of variability). Impure functions are still necessary in real programs — reading a file, updating a UI, or writing to a database are all side effects — but isolating that impurity behind a thin layer and keeping the bulk of business logic pure makes programs dramatically easier to reason about. Pure functions can be tested with plain input/output assertions and no mocking, safely memoized since repeated calls with the same input are redundant, and executed in parallel or in any order without risk of race conditions, because they never touch shared mutable state. Purity is central to functional languages like Haskell, which enforces it via the type system, and to functional idioms adopted broadly in JavaScript, Python, and other mainstream languages, especially in state-management libraries such as Redux, where reducers are required to be pure functions, and in React, where component render functions are expected to be pure with respect to props and state. The concept also underpins optimizations such as compiler-level memoization, safe code hoisting, and simplified dependency tracking in reactive frameworks, since a pure function's result can be cached or skipped entirely if its inputs haven't changed.
Key Concepts
- Deterministic: identical inputs always produce identical outputs
- No side effects — no mutation of external state, I/O, or network calls
- Referentially transparent, so calls can be substituted with their return value
- Does not depend on external mutable state such as globals or object references
- Trivial to unit test — no mocks, stubs, or setup/teardown required
- Safely memoizable and cacheable based on input alone
- Safe to run concurrently or in any order without race conditions
- Enforced by design in Haskell and encouraged idiomatically in JavaScript, Python, and Rust
Use Cases
Frequently Asked Questions
From the Blog
Extract function refactoring: when to split and when to stop
Extract by intention, not by line count. A function earns its existence when its name tells the reader something the body does not. Learn the test a candidate extraction must pass, what a long parameter list is telling you, and the symptoms of having gone too far.
Read More ProgrammingTesting Python Code with pytest: A Beginner's Guide
Untested code is legacy code from the moment it's written. This guide explains how to write effective Python tests with pytest — from your first test function through fixtures, parametrize, mocking, and measuring coverage.
Read More AI & TechnologyFunction Calling and Tool Use in LLMs
Function calling lets a language model request real actions like API calls or database lookups, turning a text generator into a system that gets things done.
Read More ProgrammingRecursion Explained With Simple Examples
Recursion is when a function solves a problem by calling itself on smaller pieces. Learn how it works, why it needs a base case, and when to use it.
Read More