Design Pure functions

Pradnya Borkar
3 min readOct 2, 2020

--

Pure function

Nowadays Functional Programming is getting a lot of attention due to the advantages it offers like parallelism, easier testing, predictability and many others.

Pure functions is a concept mainly used in functional programming languages but it can be applied in any programming paradigm

Definition

Pure functions can be defined as

  • a function which always returns the same output when called with the same argument values. The output is only determined by its input values.
  • a function that has no side effects like modifying an argument or global variable or outputting something.

Characteristics Of Pure Function

  • Function should always return a result.
  • Function must only depend on their inputs. That means this function should not use any other value other than the function inputs to calculate the result
  • Pure functions cannot have other side effects than the computation of its return value.
  • Referentially transparent: An expression is said to be referentially transparent if it can be replaced with its corresponding value without changing the program’s behaviour.

Examples

If you are a developer, you must come across some common functions we use in programing like strlen(), sqrt(),cos(x) etc. Math.cos(x) , these are the examples of Pure Function, these functions always returns the same result for the same value of x

Pure function might look like this:

fun multiply (a: Int,b :Int) = a * b

“If a is 4 and b is 5, the result is always 9, no matter how often or how fast you call it, even if done so concurrently.”

As a counter example, this would be an impure function:

int c = 5;fun multiply (a: Int,b :Int) = a * b * c

In the above example the output depends on the variable c, which is not in scope of pure function.

Benefits of pure functions

Pure functions are easier to combine

It is common practice in FP to combine many smaller pure functions into a simple solution.

In pure functions “output depends only on input”, pure functions are easy to combine together into simple solutions.For example, you’ll often see FP code written as a chain of function calls, like this:

val x = doThis(a).thendoThis(b)
.thendoThis(c)
.doThis(d)
.andFinallydoThis(e)

Pure functions are easier to test, debug

Since pure function only operates on input values, there is no other dependency hence it makes pure functions very easy to test, as we only need to test that the inputs produce the desired outputs. We do not need to check the validity of any global program state in our tests of specific functions.

Pure functions are easier to debug than impure functions. Because the output of a pure function depends only on the function’s input parameters and your algorithm, you don’t need to look outside the function’s scope to debug it.

Pure functions are easier to parallelize

Pure functions are easier to parallelize.You can easily distribute the input values over a number of threads, and collect the results.

Referential transparency

An expression is said to be referentially transparent if it can be replaced with its corresponding value without changing the program’s behavior.

To achieve referential transparency, a function must be pure. This has benefits in terms of readability and speed. Compilers are often able to optimize code that exhibits referential transparency.

Memoization(Caching)

Caching and reusing the result of a computation is called memoization, and can only be done safely with pure functions.

Because a pure function always returns the same result when given the same inputs, a compiler (or your application) can also use caching optimizations, such as memoization.

Pure functions can be lazy

Laziness is a major feature of functional programming. We only ever need to compute the result of a pure function once, but what if we can avoid the computation entirely? What if you never use the output value of pure function? As the function can not cause side effects, it does not matter if it is called or not.You can write a program to be lazy and optimize the call to pure functions away.

--

--