Liam Tait

A place where I talk about programming, magic and games

1 post under

What is a Pure function? Explained simply with examples

Pure functions are deterministic Pure functions only care about their input They keep state local. Pure functions do not reference any external values. This means that pure functions will always produce the same output based upon the input. Pure functions are deterministic. Not referencing anything but the input makes the function much easier to use, as it works in a vacuum, the function can be worked on and understood with no knowledge of the surrounding program. Purely looking at the function as a map of input to output. A deterministic function function addDeterministic(firstInput, secondInput) { return firstInput + secondInput; } A non-deterministic function var secondInput = 1; function addNonDeterministic(firstInput) { return firstInput + secondInput; } Deterministic functions are predictable. Relying only their input means that input...

Read More