Functional Programming is Fun!! (PartÂ 1)
Published: 2017-05-16 17:20:30

---
title: Functional Programming is Fun!! (PartÂ 1)
published: true
description: 
cover_image: https://thepracticaldev.s3.amazonaws.com/i/xrgq7d9fycjzj94vc4ob.png 
tags: 
---
_This article was first published on Medium. You can take a look at it [here](https://medium.com/techspire/functional-programming-is-fun-part-1-7b3d81e1b97c)_.

Actually Functional programming is more than just being fun.

> _Functional programming concepts are important even if someone programs in imperative or object oriented languages._

It has many concepts which can be used in the program irrespective of the language a person programs in.
Let’s take it one by one.

**When are functions called pure?**

![alt text] (https://cdn-images-1.medium.com/max/800/1*X-IznQeGUHls3LlMPZFCGg.png)

A function is called pure if it always returns the same result for same argument values.
Let’s understand it through an example
```python
x = 5    # global variable

sum_impure(y){
    y = x+ y    # adds the value of global variable
    return y
}
```

Code Block 2
```python
sum_pure(a, b){
    return a + b
}
```
Above written 2 functions may look similar but they are not.
Following example explains the difference:
```python
x = 5 
sum_impure(3)    # returns 8

x = 6            # changed value of global variable to 6
sum_impure(3)    # returns 9
```
`sum_impure` is an impure function because giving same input 3 as argument to it, it gives different output in each call.

But this is not the case with `sum_pure` function.

```python
sum_pure(4, 9): 13
sum_pure(4, 9): 13
```
Given 4, 9 as arguments to the `sum_pure`, it will always return 13 no matter when and where it is being called in the program.

*Do you get the difference between `sum_impure` and `sum_pure` function?*

`sum_impure` function uses the value of global variable to calculate output but if some part of the program changes the value of global variable, the output of the `sum_impure` function also changes for same input.

`sum_pure’s` output does not depend on the state of the program. It just acts on the input without requiring any information other than arguments to it from the program in which it is being called.



###But why shall I make my functions pure?

####Easier Testing
It is not easy to test `sum_impure` function because `sum_impure` function has to know the value of global variable x to execute. This is also known as state or context associated with a function.

Secondly, you can’t compare `sum_impure`’s result with some fixed value because it’s return value will change depending on the value of global variable x.

*Now are you getting why we mock databases?*

If a database read/write is there in a function definition, we can’t test that function without mocking the database.
**Databases are like state and the result of the function doing database read/write depends on what is there in the database during execution.**

> _Impure functions need state to run and test._

####Predictability
We can reason about what a pure function is doing because its results are predictable. Given same input, it always give same output.

> _Result of a pure function does not depend on the state of the program._

Result of `sum_impure(3)` is unpredictable. So if `sum_impure` is being called at 2 places in the program with same arguments, it may happen that it gives different results at these 2 places.

```python
x = 4
sum_impure(3)    # returns 7

x = 7
sum_impure(3)    # returns 10
```

There are many more advantages like parallelization, memoization associated with pure functions which we will learn in next article of this series.

*If you liked the article, please share it with others.*