The Heart of FP โ Pure Functions
Lesson 2: The Heart of FP โ Pure Functions
Everything in functional programming flows from one idea: the pure function. A pure function has exactly two properties:
- Deterministic โ the same inputs always produce the same output.
- Side-effect free โ it never touches anything outside its inputs and return value.
// IMPURE โ mutates external state and prints
let total = 0;
function addToTotal(x) {
total += x; // mutates a variable outside itself
console.log("added " + x); // I/O side effect
return total;
}
// PURE โ nothing but input โ output
function add(a, b) {
return a + b; // that's it
}
Side Effects: The Hit List
Anything a function does besides returning a value from its inputs counts as a side effect:
- Mutating a variable, array, or object outside the function
- Writing to disk, the network, or a database
- Printing to the console
- Reading the current time or a random number
- Changing the DOM or global configuration
Each of these makes behavior depend on when and where the function runs โ which is exactly what makes bugs hard to reproduce.
Referential Transparency
An expression is referentially transparent when you can replace it with its value without changing the program's behavior. Pure functions give you this property, which means you can reason about code like algebra: if double(21) always equals 42, you can substitute freely. This is what makes refactoring safe and debugging surgical.
Why Purity Wins
| Concern | What purity gives you |
|---|---|
| Testing | No mocks, no setup, no cleanup. Call it, assert on the return value, done. |
| Predictability | A function can't secretly change the state of your app or library. |
| Caching | Same input โ same output, so results can be memoized safely. |
| Parallelism | Pure functions share nothing, so they can run on multiple cores without locks. |
| Composability | Functions that don't meddle combine reliably โ the topic of Lesson 4. |
๐ง Knowledge Check
1. Which of the following is a pure function?
2. "Referential transparency" means:
3. Functional programs still do I/O and talk to databases. How is that reconciled with purity?