# Functional Programming: Pure Functions

## Learn the heart of functional programming, Pure Functions.

Assalam u Alaikum & Hello Everyone! ๐

I hope you all are good โจ๐ค

In the last article, we learned about Immutability & how it helps us in functional programming.

In this article, we will learn about the heart of functional programming i.e Pure Functions.

### Contents

• โก What Is A Pure Function?
• โก Side Effects?
• โก Same Input, Same Output
• โก Pure Functions & Functional Programing

### What Is A Pure Function?

A function is a Pure Function when it has the following two qualities:

1. No Side Effects
2. Given the same input, it should always return the same output.

Let's take a look at both of these qualities in detail now ๐

### Side Effects?

When a function relies on or modifies anything outside of its body (to perform a task) it creates a Side Effect.

For example, a function that reads or writes from a variable outside its own arguments, a database, a file, or the console can be described as having Side Effects.

Eric Elliott has compiled a list of our day to day Side Effects that include:

• Modifying any external variable or object property (e.g., a global variable, or a variable in the parent function scope chain).
• Logging to the console
• Writing to the screen
• Writing to a file
• Writing to the network
• Triggering any external process
• Calling any other functions with side-effects

#### Example:

Let's take a small example to understand the concept of Side Effects. x

``````let count = 0;
function add(x, y) {
count++; // โ Side Effect performed.
return x + y;
}
``````

In the above function, we can see that the function has a simple duty, add `x` & `y`. But the function `add` is also changing the state of another variable `count` which is outside of its body. This is one of the simplest examples of Side effects.

A Pure Function can never have side effects. That means it should never rely on or modify anything outside of its body.

### Same Input, Same Output

A function that takes some arguments will always return the same value. This concept is tricky at first but it's simple.

Let's just understand it by the example which we have seen above.

``````function add(x, y) {
return x + y;
}
``````

Here, we have the `add` function, if we call it with `2` & `3` it will always give us the result of `5`.

``````add(2, 3); // 5
add(2, 3); // 5 -> Same input (2, 3), Same Output (5)
add(2, 3); // 5 -> Same input (2, 3), Same Output (5)
add(2, 3); // 5 -> Same input (2, 3), Same Output (5)
``````

Now, Let's modify the `add` function and make it impure by violating the rule of Same Input, same Output.

``````function add(x, y) {
return x + y + Math.random() * 100;
}
``````

If we test the `add` function with the same arguments `2` & `3` now, the result will be different every time.

``````add(2, 3); // 101.1277034641613
add(2, 3); // 18.61676254278017
add(2, 3); // 49.983132551339345
``````

Our `add` function is no more a pure function now because it's not producing the same output when the input is the same.

I can understand that you will never do this in the real world. But this is just an example or simulation to understand the concept.

We will come later down into the real world examples when we will cover the Function Composition. Hang on with me! โค๏ธ

### Pure Functions & Functional Programming

As said at the start, Pure Functions are the heart of functional programming. They help us to distribute the tasks in small and independent programs.

Through pure functions, we avoid side effects and do not mutate the things that are not in our hands. Secondly, with the same input/output principle, our state of function becomes more predicted.

We will see the real power of Pure Functions in the next article when we will learn how to compose different functions to perform tasks.

### Stay tuned ๐

That's it, folks! hope it was a good read for you. Thank you! โจ