Pure Functions-- What's all the craze about? 
Published: 2020-09-23 14:08:12

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/wovjg1o2fsaekxx48ca6.jpg)
##**Pure Functions** 
Let’s talk about pure functions and what exactly makes them “so pure.” In order to do this we need to take a deeper look at functions in general. Functions can be divided into two groups: functions “that are called for their side effects” and “those that are called for their return value” (Haverbeke, Eloquent Javascript). A side effect occurs when there is a change in the application that “is observable outside the called function other than its return value" (Elliot, Master the Javascript). 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/j9wuz2iv8d1r6in7gsir.jpg)

Delving deeper into the land of side effects and functions, some functions create values while other functions are called for their side effects. It is best code practice to use functions that create values  rather than functions that perform the side effects. Functions that do not have side effects are easier to combine. Some examples of side effects include: “modifying any external variable or object property (e.g., a global variable, or a variable in the parent function scope chain) logging values to the console, writing to the screen, writing to a file, writing to the network, triggering any external process etc” (Elliot, Master the Javascript). Ultimately, the less side effects we have in our code, the easier our software will be to debug, test and maintain. This leads us to further discuss pure functions. 

###**Pure Function Example** 

Pure functions are functions that always return the same value for the same inputs and pure functions produce values in which there are no side effects. 

 ![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/w1k0gkr4z8fumciikg07.png)

The above function is *pure*. The function produces **no** side effects.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/9slebvkdv67jdei4kwog.png)

ImpureFunc is an impure function. Its output is not dependent on the input value. We can change the above function and make it pure. 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/pso3zorqdeup2r77milz.png)

The above function is now pure because it did not produce any side effects. It also returned an output based on the input value.


To recap, pure functions always return the same value for the same inputs and do not rely on side effects from other codes. A pure function will not alter any input or value in the global scope. Never alter a variable in the global scope when ensuring functional purity. Thank you for reading my quick review of pure functions. 


####**Sources**
Haverbeke, Marijn. “Chapter 3Functions.” Functions : Eloquent JavaScript, eloquentjavascript.net/03_functions.html. 

Elliott, Eric. “Master the JavaScript Interview: What Is Functional Programming?” Medium, JavaScript Scene, 2 July 2019, medium.com/javascript-scene/master-the-javascript-interview-what-is-functional-programming-7f218c68b3a0. 

Chidume Nnamdi. “Understanding Javascript Mutation and Pure Functions.” Medium, Bits and Pieces, 20 Aug. 2019, blog.bitsrc.io/understanding-javascript-mutation-and-pure-functions-7231cc2180d3.