Functional Programming Programming

Pure Functions | The path to less headache – Functional Programming

October 8, 2018
Pure Functions

An important part of functional programming is pure functions. Before we try to write our code in functional programming style, we must need to know what is pure functions and why do we need it.

Pure Function

A pure function is a function that always gives the same output if we give the same input. It means no matter what, in any circumstances, if the input is the same then the returned value from the function will be the same. Also, they should not have any observable side effects. We will talk about side effects later. Right now let’s focus on how to make a pure function and what it takes to be a pure function.

Who is Pure?

As I’ve said earlier, it will give the same output for the same input. So we can assume that a pure function is like a self-sustaining noble person who does not depend on anyone nor he favors anyone. No matter what happens outside of his world he always stays the same and does his damn job like always. Likewise, for pure functions, we can say, this kind of functions does not depend on any variables outside of its scope and does not change (read: mutate) any outside variable or data. Let’s explain this with an example.

Let me give you an example using 2 JavaScript methods. they are

  • slice()
  • splice()

slice() method returns the selected element in an array, as a new array. (learn more here, if necessary)

splice() method also returns the selected element in an array, as a new array. (learn more here, if necessary)

The difference is on how they work! slice() function is a good function who does not harm anyone, I mean he only gets the data from an array but while doing that he does not mutate(change) the original array. On the other hand, his evil brother splice() function mutate(change) the original array while getting the desired data.

Let’s say, we have an array. fruits = [“banana”, “orange”, “apple”, “mango”]

Did you See what splice() did? From this example, we can say that splice() function does have some Side Effects. Although slice() and splice() does the same thing upon running the function multiple times we can see that they start to return different data. In functional programming, we always try to use slice() function. On the other hand, we should always avoid splice() function, as it mutates ( changes) data.

Side Effects

Let’s look at another example. In our country, the minimum age for opening an account is 18 years for both men and women. So we write this code

Now let’s say a new (Rockstar | Ninja | Cool)  coder comes to your team. He is assigned to create a new function that stores the lowest price of a product. He doesn’t have so much time to read all 2144 lines of code. So he writes this code.

His fault was while writing code he forgets to create a scoped variable thus storing the data in the global variable. Those of us who have faced this problem only knows how much pain it is to find the bug. After finding out, everyone might (bash | beat | etc) that ninja coder, but I think the person who created the global variable should be held responsible and be punished in the dungeon. (just kidding! or am I?) We shouldn’t create any variable globally at all. Because this might create a situation where a variable might be mutated(changed) by other functions unintentionally.

Now the first question comes to mind after reading this, what should we do when we had to share certain variables between multiple functions? In Functional Programming, we should always avoid data or objects that are shared between multiple functions. Then how do we share? We will talk about that in later part.


Right now we should only keep this in mind that, a pure function is completely self-contained like a portable versioned software. We just give the input and the pure functions give us the output. Because of handling the functions this way, we know what a function is doing under its hood. We could know the dependencies of that function, about what it needs and what it will give us as an output. As in JavaScript, functions are like values so we need functions to be as much portable as we can. So that we can run it anywhere, whether we run it in a nodeJS project on a server or in the browser as (react | angular | etc) application.

The problem with object-oriented languages is they’ve got all this implicit environment that they carry around with them. You wanted a banana but what you got was a gorilla holding the banana and the entire jungle.

Joe Armstrong. Creator of Erlang


One other thing with the pure function is that the testing of the function is really easy. As it does not mutate any variable or data outside of its functions so we don’t have to check/assert the whole program’s data/state. We simply give functions the input and assert the output.

Referential Transparency

1The term “referential transparency” comes from analytical philosophy, the branch of philosophy that analyzes natural language constructs, statements, and arguments based on the methods of logic and mathematics.   A context in a sentence is “referentially transparent” if replacing a term in that context by another term that refers to the same entity doesn’t alter the meaning. For example

“The Scottish Parliament meets in the capital of Scotland.”

means the same as

“The Scottish Parliament meets in Edinburgh.”

So the context “The Scottish Parliament meets in …” is a referentially transparent context.

2In terms of programming, a part of the code is referentially transparent when it can be substituted for its evaluated value without changing the behavior of the program.  Here is an example of a referentially transparent function:

With a referentially transparent function, given an input and a function, you could replace it with a value instead of calling the function. So instead of calling plusOne with a parameter of 5, we could just replace that with 6.

Thread Safe

As our pure functions do not need to access any shared memory, so they can run simultaneously in any environment, whether it’s running in a server or running on a browser of a client PC using web workers.




You Might Also Like

No Comments

Leave a Reply