What is purity?
When talking about a purely functional programming language, we are talking about a language in which each function adheres to these properties:
- Does not generate any side effects
- Returns the same output when providing the same input (idempotence)
This means that our functions are completely deterministic.
The best way forward might be to demonstrate what we are talking about by showing some examples. So, in this section, we’ll take a look at two functions, a pure one and another which is impure. Then, we’ll talk a bit more about the properties of such functions and their importance to the programs that we are writing.
Demonstrating pure versus impure function calls
A simple example of this would be an addition function. This is a function that takes two integers as input and returns the sum as the output:
func add(a, b int) int { return a + b }
When we call this function with the same inputs, we will get consistent output...