Mark As Completed Discussion

Pure Functions

Pure functions make functional programming a reality. So, what are these pure functions? Let’s dissect the term into two here. The second word refers to functions, as with any programming language. They take an input, perform some computation and give an output.

JAVASCRIPT
1const impure = (a) => {
2    return Math.random()*a;
3}

The above function is impure as it takes an input number a and returns a random number multiplied with a. So, how does a function become pure? The purity of a function is described by the results that it computes when given an argument. The result should always be the same when the arguments are the same, no matter how many times it has been passed to it. Here is a simple example of a pure function.

JAVASCRIPT
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment