Show Buttons
Share On Facebook
Share On Twitter
Share On Google Plus
Share On Linkdin
Share On Reddit
Share On Stumbleupon
Contact us
Hide Buttons

Pure vs Impure functions

Char­ac­ter­estics of Pure Functions

  • The return value of the pure func­tions solely depends on its arguments
    • Hence, if you call the pure func­tions with the same set of argu­ments, you will always get the same return values
  • They do not have any side effects like net­work or data­base calls
  • They do not mod­ify the argu­ments which are passed to them

For exam­ple,

function calculateSquareArea(x) {
   return x * x;
}

Char­ac­ter­isitcs of Impure functions

  • The return value of the impure func­tions does not solely depend on its arguments
    • Hence, if you call the impure func­tions with the same set of argu­ments, you might get the dif­fer­ent return values
    • For exam­ple, Math.random(), Date.now()
  • They may have any side effects like net­work or data­base calls

  • They may mod­ify the argu­ments which are passed to them

For exam­ple,


function squareAll(items) { var len = items.length; for (var i = 0; i < len; i++) { items[i] = items[i] * items[i]; } return items; }

You may also like...