A side effect is anything a function does besides returning a value. Sending an email, reading a file, modifying global state, making a web request, or blinking a light all count. These interactions are often the reason software exists, so functional programming does not eliminate them; it makes them easier to use and control. section 1.1 section 1.3 section 1.4
Use this distinction to reason about a function:
- The explicit inputs are its arguments, and the explicit output is its return value. section 4.7
- Information entering or leaving through any other channel is implicit and becomes a side effect. section 4.7
- Side effects happen whenever the function is called, which can cause unintended behavior when only the return value is wanted. section 1.1
- Replacing implicit inputs with arguments and implicit outputs with return values turns an action into a calculation. section 4.7
State changes alongside a return value
In add_to_total(amount), the amount is an explicit input and the returned total is an explicit output. Reading and modifying the global total, plus logging the old total, are additional interactions: side effects. section 4.7
var total = 0;
function add_to_total(amount) { #1
console.log("Old total: " + total); #2
total += amount; #3
return total; #4
}
Inspect the function listing to separate its argument and return value from global-state access and console output. section 4.7























