Functional Programming is Fun !! (Part 1)

Saurabh.v
Techspire
Published in
3 min readDec 22, 2016
Pure fun !!

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 them 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 following example:

x = 5    # global variablesum_impure(y){
y = x+ y # add value of global variable to argument y
return y
}

Code Block 2

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

Above written 2 functions may look similar but they are not.
Following code block 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 the 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 we make our 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 a 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 gives the same output.

The result of a pure function does not depend on the state of the program.

The 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 click on the like button and share it.

--

--

Saurabh.v
Techspire

bits_pilani_alumnus, software _engineer at make_my_trip, functional_programming_enthusiast, experience_seeker, learner