Functional programming is getting lot of attention these days due to the advantages it offers like , , and many others. parallelism easier testing predictability is a concept mainly used in functional programming languages but it can be applied in any programming paradigm. Pure functions What are pure functions ? There are two conditions that a function must satisfy to be called _pure._Let’s understand these two conditions one by one. Given same argument to the function, it should always return same output. How can we be certain that above mentioned point holds while writing pure functions? Follow me to get the answer! Function must not use state of the program to compute its output. x = 5 # x is a global variable multiply(y):return y*x is not a pure function because its output is computed using global variable x. If the value of x changes, output of function also changes for same input. multiply multiply x = 5multiply(2) # returns 10 x = 10 # value of global variable x changed from 5 to 10multiply(2): 20 In above example input to function is same but output has changed depending on the state of the program. multiply Global variable x is also known as the state of the program because it can be accessed in any part of the program and defines the data associated with a program during its execution. Since **multiply** function is not giving same output for same input, it is not pure. To make pure, we can pass global variable x as argument to it. multiply def multiply_pure(y, x):return y*x multiply_pure(2, 3) # returns 6multiply_pure(2, 3) # returns 6 No matter how many times we call with same input, it will always return same output. multiply_pure Secondly, a function must not take mutable objects as arguments and should not use it to compute output of the function when working in a concurrent programming environment. This is because concurrent processes can modify mutable object and ultimately modify the output of a function.Following example illustrates the above mentioned point: # returns length of the list passed as argumentdef compute_length(elements):return elements.length() Mutable list being modified by multiple processes elements In between the time when argument is passed to and length of is returned by function, it may happen that the length of list is modified by another process. This is because is a shared mutable list which can be accessed by multiple processes. elements compute_length elements compute_length elements elements Hence is not pure in concurrent programming environment. compute_length Thirdly, a function must not take any input from the I/O to be pure. # takes input from the user and turns it into a greetingdef greet():str = raw_input() # take input from userstr = "Hello " + strreturn str In above written example, output of the function may change depending on the input taken from the I/O. Evaluation of the result should not cause any side effects such as mutation of mutable objects or output to I/O devices Pure function should not mutate any mutable object. Let’s follow below written example to validate statement written above: class Box:def __init__(self, length):self.length = length # doubles length of Box object# Assumption: Only box objects are passeddef double_length(box):box.length = box.length * 2return carton = Box(5) # a Box object is createddouble_length(carton) # length of carton is doubledprint carton.length # prints 10 Here function is impure as it modifies mutable object. double_length Box Doubts ?? Let’s understand it step by step is passed by reference to function in above example. carton(Box object) double_length Any changes to object inside the function will be visible outside the function. So we can say that object is accessible to whole program, which means that object is nothing but state of the program. Box Box Box And a function must not modify state to be called pure. Pure function should not output to I/O. # prints textdef printer(text):print “printing…. ” + text I/O can be print on a console, write to a database etc. Conclusion Any function which interacts with the state of the program is a potential candidate for causing side effects. State of the program can be global variables, mutable objects, I/O operations etc. And any function which causes side effects is not pure !! Please hit the ♥ button if you liked the article so that other Medium users might find it and dig it too.