What is a Pure Function?
There are two parts to this term.
- Pure
- Function
I am assuming that you already have an answer to the following question.
What is a Function?
But let's quickly look at the definition of a function from the mathematics. We want to go back to the mathematical description because all mathematical functions are always pure. Let's look at the below figure from the math-is-fun portal.
So, there are three main parts of a function.
- The input
- The relationship
- The Output
The Fig.1 shows an example for input, relationship and the output. The relationship is the most critical part of a function because it is the relationship that determines the output based on the input. From a programmers perspective, the relationship is the body of a function code. We can write Scala code for example shown in Fig.1. The Scala code is as small as shown below.
The code written inside the curly braces is the relationship. Let's analyze this small code and draw some observations.
- The Input solely determines the output.
There is no other thing like a global variable or the content of a file, or input from the console that determines the output. It is only the input parameter value, nothing else. No matter how many times or where do you invoke this function, as long as the input parameter value is same, you are going to get the same output. That is the first feature of a Pure Function. - The function does not change its input.
The above code snippet takes i as an input and uses it to calculate the output. However, it does not change the value of i. A pure function guarantees that the input value remains unchanged. It never modifies the Input value. That is the second quality of a Pure Function. - The Function does not do anything else except computing the output.
The above code snippet does not read anything from a file or console. It does not print anything on the console or write some data to a file. It does not read or modify a global variable or for that matter anything outside the function. In fact, it does not perform any I/O. A pure function is similar to a coffee machine. Takes the input, computes the output and returns it. It does not do any other work. If it does anything else that impacts the outside world or is visible to outside world, we call it a side effect of the function. A side effect is like doing something other than your primary purpose. So, a function is pure if it is free from side effects. That is the third quality.
That is all. We can summarize these rules as below.
Pure Function Rules
- The Function input solely determines the Function output.
- The Function does not change its input.
- The Function does not have any side effects.
If a Function qualifies for above three conditions, it is a pure function.
Is there any other easy method to validate the purity of a function? Continue reading to get the answer.
Read More
Pure Functions | Referential Transparency | Benefits of pure functions | First class functions | Higher order function | Anonymous functions | Immutability | Tail Recursion | Expressions in Scala | Lazy Evaluations | Pattern Matching | Closures