DELAPARRA
Index
Color
NL
Category: Programming paradigms

Pure Function

Explained by Updated

What is Pure Function?

Pure Function: A function with no side effects: given the same inputs, it always returns the same output and never modifies external state. add(2, 3) is always 5 — no matter when or how often you call it. A pure function always returns the same output for the same input and has no side effects. It does not change global variables, write to databases, send emails or depend on the current time. add(2, 3) is always 5.

Definition source Coders Lingo, CC BY 4.0

Frequently asked questions about Pure Function

How is Pure Function used in practice?

Pure functions are the easiest code to understand and test. You pass input and check output, without setup, mocks or cleanup. They are also safe to cache and to run in parallel.

What should you keep in mind with Pure Function?

Real applications need side effects, but you can push them to the edges. Keep the core calculations pure: price rules, discount logic, date calculations and validation. Then let a thin outer layer read from the database, call the pure functions and save the result. That structure makes the most important logic thoroughly testable and easy to change.

At a glance

CategoryProgramming paradigms
Explained by
Updated

More Programming paradigms terms

All terms starting with P

Skills

Download all skills (zip)

Manuals

Related services

Need any help?

Stuck on one of these in your own project? Tell me what you are working on and I will tell you how I would approach it.