An explicit output is information a function deliberately makes available through its return value. Arguments provide explicit inputs; other information entering or leaving the function remains implicit. Replacing implicit outputs with return values turns actions toward calculations. section 4.7
Use explicit outputs to make computations easier to test and reuse:
- Return an answer instead of writing it directly to the DOM. section 4.8
- Return a new value instead of modifying a global array. section 4.10
- Pair returned results with explicit arguments to remove hidden dependencies. section 4.7
Returning a calculation
A tax update can keep the DOM effect at the boundary while returning the calculation from a separate function: calc_tax(amount) returns amount * 0.10, and update_tax_dom() passes that result to set_tax_dom(). section 4.10
var total = 0;
function add_to_total(amount) { #1
console.log("Old total: " + total); #2
total += amount; #3
return total; #4
}
Compare the returned total with the function's other output: logging and global-state mutation remain implicit, while return total is explicit. section 4.7
