This article is a continuation of my previous article that i wrote on functional programming. If you haven't gone though it, here is the link.
So, what are pure functions and how do they relate to functional programming? Great question that i hope to answer to you satisfaction by the end of this article. If not i apologise in advance. Sorry.
A pure function is a function (block of code in JavaScript that processes a given parameter when invoked), that always returns the same value if the same parameters are passed. This is not dependent on any state or data change during a programs execution but rather on the parameters put). A dead and direct giveaway that a function is impure is if it makes sense to call it without using its return value. For pure functions, that’s a no no. Wanna see a code example? Here you go.
var testMarks = 20;
let calculatePercentage = ( catMarks ) {
return catMarks + (testMarks / 70);
}
I don't know my audience so i'm just going to assume that you, dear reader, are a newbie and explain what this code does. The above code snippet calculates total student marks and the function takes their CAT marks as the parameter and returns the total marks out of 100 assuming the testMarks is less than or equal to 30.
Now that that is out of the way, can you guess if it is a pure function or not?
Yaaayy! You are right. Unless you are not in which case there is no need to worry because you are here to learn. The code snippet is not a pure function. This is because it is dependent on an outside variable 'testMarks' and in the instance it somehow changes, we will get a different result even though we pass the same 'catMarks' as the parameter.
Below is an example of a pure function.
let pure = (input) => {
let a = 1;
// Multiply with variable inside function scope
let output = input * a;
return output;
}
The function, pure does not rely on any variable outside its scope.
Pure functions posses the following characteristics.
They do not modify the state of the application. (they have no side effects)
They do not rely on any variable outside their scope.
With a given input (parameter), always return the same output (result) - are deterministic.
Side effects in functions.
A side effect is any change in the system that is observable to the outside world or in our case, you the programmer as you write your code. Some side effects include:
Making a HTTP request.
Mutating data.
Printing to a screen or console/logging.
DOM Query/Manipulation.
Getting the current time.
Changing the file system.
Inserting a record into a database.
Obtaining user input.
Importance of pure functions.
The most notable use of pure function is referential transparency. This is generally defined as an expression in a program, that may be replaced by its value (or anything having the same value) without changing the result of the program. This basically implies that methods should always return the same value for a given argument, without having any other effect on the output. Let's use a code example to understand this concept.
Source: Techaffinity
let a = (num1,num2) => {
return num1+num2;
}
let b = (num) => {
return num*3;
}
console.log(b(a(6,4))) //output will be 30
// here you can replace a(6,4) expression with value 10 and this will not effect to the result of the program because it returns the same value: 10
// Hence you can replace console.log(b(a(6,4))) to console.log(b(10)) as the function is referentially transparent.
let c = (num1,num2) => {
console.log(`Value of num1 is:${num1} and value of num2 is:${num2}`);
return num1+num2;
}
console.log(b(c(6,4))) //output will be 30
// here you cannot replace expression c(3,4) with value 10 as it affects the result of the program
// function c has console.log(), which is one type of side effect. So, the function ‘c’ is not referentially transparent
In the above example we have two functions: hoFunc and hoFunc2. Since the hoFunc function returns a function, it is a Higher-Order Function. On the other hand, hoFunc2 accepts function as argument, which is again a Higher-Order Function.
Other notable advantages of pure functions include:
Readability - One problem of side effects due to impure codes is that it makes it difficult for us to understand codes. The non-deterministic nature of impure code is that it might return several value for a given input. This is eliminated in pure functions.
Testability - writing tests for pure functions is a lot easier.
Reusability - because pure functions only depend on the inputs you feed them, so they are reusable in other codebases and in different projects.