{"post":{"slug":"what-is-a-pure-function-2026","title":"What Is a Pure Function? 2026 Guide","excerpt":"A pure function always returns the same output for the same input and never touches anything outside itself. Here is why that narrow definition is disproportionately useful in real code.","category":"code","date":"July 20, 2026","author":"ByteLedger Team","content":"<span class=\"dropcap\">A</span> pure function follows exactly two rules: given the same input, it always returns the same output, and it never causes a side effect anywhere else in the program. No reading a global variable, no writing to one, no network call, no mutating an argument, no console output. That definition sounds narrow, but functions that follow it are disproportionately easy to test, cache, and reason about — which is why they sit at the center of functional-style programming.\n\n## What changed in 2026\n\n- **Linters catch impurity earlier.** More static analysis tooling can flag likely side effects — mutated arguments, reads from outer scope — directly in the editor, rather than surfacing them as a bug report weeks later.\n- **AI code generation makes purity easier to request explicitly.** Prompting an assistant for \"a pure function that does X\" is now a common, well-understood pattern, and most current models produce genuinely side-effect-free code when asked directly.\n- **Memoization libraries got easier to drop in.** Caching a pure function's results now takes a single utility call in most ecosystems, rather than a hand-rolled cache object, which raised the payoff of writing functions purely.\n\n## The precise definition, with real code\n\n```js\n// Impure — depends on outside state, so the result can change across calls\nlet taxRate = 0.08;\nfunction priceWithTax(price) {\n  return price + price * taxRate;\n}\n\n// Pure — everything the function needs comes in as an argument\nfunction priceWithTax(price, taxRate) {\n  return price + price * taxRate;\n}\n```\n\n```js\n// Impure — mutates the argument it was given\nfunction addItem(cart, item) {\n  cart.push(item);\n  return cart;\n}\n\n// Pure — returns a new array; the original is untouched\nfunction addItem(cart, item) {\n  return [...cart, item];\n}\n```\n\nThe second version of each function is testable with nothing but plain values — call it, check the return value, done. The first version requires tracking and resetting whatever outside state it touches.\n\n## Referential transparency\n\nThe formal name for \"same input, same output, no side effects\" is referential transparency: a call to a pure function can be replaced by its return value anywhere in the code without changing what the program does. This is what allows a compiler, or a human reading the code, to reorder, cache, or skip repeated calls safely. Impure functions cannot be treated this way — calling one twice might behave differently, or matter for reasons beyond its return value.\n\n## Pure vs impure, side by side\n\n| Property | Pure function | Impure function |\n| --- | --- | --- |\n| Output depends only on arguments | Yes | Not guaranteed |\n| Safe to cache (memoize) | Yes | Only with care, if ever |\n| Testable without mocks | Yes | Often needs setup or mocking |\n| Safe to call from multiple threads | Yes | Needs synchronization if state is shared |\n| Can log, save, or fetch | No | Yes, that is the point of many of them |\n\n## Why total purity is not the realistic goal\n\nEvery useful program eventually reads a file, calls an API, or writes to a database — all inherently impure. The practical goal is not zero impure functions; it is pushing impure parts to the outer edges of the program (I/O, logging, randomness) while keeping the bulk of the logic — calculations, transformations, validations — pure and testable in isolation.\n\n## How to spot an impure function hiding as pure\n\nWatch for functions that look clean but quietly break one of the two rules: reading `Date.now()` or `Math.random()` internally, reading a module-level variable instead of taking it as a parameter, or mutating an object passed in even though the return value looks fine. None of these throw an error — they just make the function behave differently across calls, which defeats the point of treating it as pure.\n\n## Common mistakes\n\n**Assuming a quick test with no visible change means pure.** A function that mutates its argument can pass a shallow test while still being impure and dangerous to call twice with the same object.\n\n**Forgetting that pure functions can still throw.** Throwing for genuinely invalid input is fine; the purity rule is about outside state and side effects, not about never raising an error.\n\n**Over-purifying code that genuinely needs to be stateful.** Not everything benefits from being forced pure — an object managing a live connection is legitimately stateful, and fighting that is often more effort than it is worth.\n\n## FAQ\n\n**Can a pure function call another pure function?**\nYes, as long as every function in the chain follows both rules. Composing pure functions is one of the main ways functional-style code is built.\n\n**Is a pure function the same as a static method?**\nNo. A static method can still read or mutate shared state; purity is about behavior, not where the function is defined.\n\n**Do pure functions make code automatically fast?**\nNot automatically, but they enable optimizations — memoization especially — that are unsafe to apply to impure code.\n\n**Can a pure function use immutable data internally?**\nIt should. Mutating a local variable that never leaves the function is technically fine, but working with [immutable data](/blog/what-is-immutability-in-code-2026) throughout tends to make purity easier to maintain as a function grows.\n\n## Where to go next\n\n- [What is immutability in code in 2026](/blog/what-is-immutability-in-code-2026)\n- [What is a higher-order function in 2026](/blog/what-is-a-higher-order-function-2026)\n- [What is currying in 2026](/blog/what-is-currying-2026)","hue":150,"metaTitle":"Pure Functions Explained 2026","metaDescription":"What is a pure function in 2026? A precise definition, real pure vs impure code examples, and why purity makes testing and memoization so much easier.","keywords":["pure function 2026","pure function definition","pure function vs impure function","referential transparency","side effects programming","pure function javascript","pure function examples","why use pure functions","functional programming pure functions"],"tldr":["**Same input, same output, always** — no randomness, no reading outside state, no exceptions to the rule, or it is not actually pure.","**No side effects, full stop** — no network calls, no mutating an argument, no writing to a shared variable, no logging inside the function itself.","**Purity is what makes memoization and easy testing possible** — cache the result by input, or test with plain input and output pairs, with no mocking required.","**Most real programs cannot be entirely pure** — reading a file or calling a database is impure by definition; the realistic goal is pushing impurity to the edges, not eliminating it.","**Skip:** calling a function pure just because it does not mutate its arguments — it also has to avoid reading outside state and avoid other side effects like logging or network calls."],"readTime":"4 min read","wordCount":910},"related":[{"slug":"what-is-a-higher-order-function-2026","title":"What Is a Higher-Order Function? 2026 Guide","excerpt":"A higher-order function takes a function as an argument, returns one, or both. Here is why that one capability underpins map, filter, reduce, and most functional-style code.","category":"code","date":"July 18, 2026","author":"ByteLedger Team","readTime":"4 min read","hue":200},{"slug":"what-is-functional-programming-2026","title":"What is functional programming in 2026? Explained","excerpt":"Functional programming builds software from pure functions that avoid changing shared state. Here is what that means, why it helps, and where it costs you.","category":"code","date":"June 2, 2026","author":"ByteLedger Team","readTime":"3 min read","hue":150},{"slug":"what-is-a-function-2026","title":"What is a function in programming in 2026? A clear guide","excerpt":"A function is a named, reusable block of code that takes inputs and returns a result. This guide explains how functions work, why they matter, and how to write a good one.","category":"code","date":"June 1, 2026","author":"ByteLedger Team","readTime":"4 min read","hue":142}],"sameCategory":[{"slug":"bazel-build-explained-2026","title":"Bazel Explained: What Hermetic Builds Actually Cost","excerpt":"Bazel delivers reproducible, precisely cached builds and demands that you describe everything. Here is what you get, what it costs, and who should say no.","category":"code","date":"August 19, 2026","author":"ByteLedger Team","readTime":"3 min read","hue":145},{"slug":"graphql-federation-2026","title":"GraphQL Federation: One Graph, Many Teams","excerpt":"Federation composes independently-owned subgraphs into a single API. Here is what it solves, the operational cost it adds, and when a simpler gateway is enough.","category":"code","date":"August 19, 2026","author":"ByteLedger Team","readTime":"3 min read","hue":300},{"slug":"hermetic-builds-explained-2026","title":"Hermetic Builds Explained: Same Inputs, Same Output","excerpt":"A hermetic build depends only on declared inputs. Here is what that property gives you, the common leaks that break it, and how to move toward it incrementally.","category":"code","date":"August 19, 2026","author":"ByteLedger Team","readTime":"3 min read","hue":130},{"slug":"monorepo-tooling-compared-2026","title":"Monorepo Tooling Compared: What You Actually Need","excerpt":"Monorepo tools solve task orchestration and caching, not repository layout. Here is what each tier provides and how to tell when you have outgrown the simple option.","category":"code","date":"August 19, 2026","author":"ByteLedger Team","readTime":"3 min read","hue":150}],"newer":{"slug":"what-is-a-pull-request-review-2026","title":"What Is a Pull Request Review in 2026?","excerpt":"A pull request review is the formal approve, comment, or request-changes decision attached to a pull request, not just the act of reading code. Here is how the review step actually works in 2026.","category":"code","date":"July 20, 2026","author":"ByteLedger Team","readTime":"4 min read","hue":310},"older":{"slug":"what-is-a-qualified-charitable-distribution-2026","title":"What Is a Qualified Charitable Distribution? 2026 Guide","excerpt":"How qualified charitable distributions work in 2026 — who is eligible, the annual limit, and why a QCD can beat a normal donation for retirees taking required minimum distributions.","category":"finance","date":"July 20, 2026","author":"ByteLedger Team","readTime":"3 min read","hue":212}}