saurabh.v

Posted on

# Functional Programming is Fun!! (PartÂ 1)

This article was first published on Medium. You can take a look at it here.

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?

A function is called pure if it always returns the same result for same argument values.
Let’s understand it through an example

``````x = 5    # global variable

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

Code Block 2

``````sum_pure(a, b){
return a + b
}
``````

Above written 2 functions may look similar but they are not.
Following example explains the difference:

``````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.

``````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.

``````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.

Good explanation, thanks!

Co-op Source

Function composition and application/execution order independence are probably stronger arguments from a practical perspective.

saurabh.v

Function composition is one of the advantages of functional programming where functions can be treated as first class citizens. Yeah application/execution order independence is what I will describe in next article of this series.

Thanks for the suggestions..