Functions: Am I pure?

Pure functions

What are functions and FP

From our beautiful memories of high school algebra or set theory we remember that a mathematical function maps the domain to range. Now, If you are like “My god! now what are these terms, I don’t recall learning any such things…”, then don’t worry we’ll quickly try to summarize these below.

square function
* domain: number
* co-domain: number
* square: x -> x * x
function square(x: number): number {
return x * x;

What purity means to a function

Even though some of the functions in programming are close representations/resemblances to mathematical functions, they’re usually not the case. As we saw above that mathematical functions are completely abstract entities, and in case of programming we usually want a function to manipulate things that are rendered on the screen, interact with some other system or maybe process a file. Another important difference to ponder is that functions have access to all the outer scope and context or even things that are completely outside its own scope and the scope of the program such as a database connection or some remote API service. As these context exist we are able to change things that are outside the control of the program. Meaning the functions in programming are substantially more complex in terms of their behavior, implementations and separation of concerns. These very differences between the two type of functions has led us to distinction between pure and impure functions.

  • modifies/mutates mutable input arguments(in case of passed by reference).
  • throwing exceptions or performing some kind of I/O operations includes things such as interacting with processes outside the application’s boundary, like interacting with database, a filesystem or a console.
function doubleNum(num: number): number {
return 2 * num;
const x = doubleNum(3); //6// should be same as
const x = 6;
// then doubleNum(num) is said to referentially transparent

Pure functions

Pure functions are the ones that closely resemble mathematical functions abstracted from the outer context they do nothing but compute an output based on their input values. No other factors are allowed have any effects on its functionality, thus no side effects makes them pure. So, In summary a pure functions

  • Output is solely determined upon the provided inputs.
  • Given same input will always produce same output(are referentially transparent).

Purity and testability

In functional programming the ideal function is the one that is pure, A pure function always returns the same output for same input and has no side-effects, Since these pure functions are independent of existence of the any external context, Because of these isolation characteristics of pure function they are quite easily testable with unit test.

Get in touch

Hey, have any suggestions, questions or concerns, Feel free Mail me. You can also find me Twitter, GitHub and LinkedIn. Help me make this better drop me a message and I’ll get back to you soon Thanks!🎉



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Jay Gurav

Jay Gurav


Software Engineer, Front-end and Back-end developer, with interest in building scalable, highly efficient, resilient and user-friendly systems.