Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between pure and impure function?

Tags:

java

accessor

I assumed that pure functions must always have a return type (i.e., must not be void) and must have the same output regardless of the state of the object and that Impure functions change the state of the object or print the state of the object.

But the textbook I use states that:

An accessor usually contains a return statement, but a method that prints information about an objects state may also be classified as an accessor.

I'm confused. Which one is correct?

EDIT

A bit of clarification,The thing that makes me ask is this question:

The last question is to "Give the type of function used", and the people who commented there stated that it is an impure function as it is printing.

So is this function pure or impure?

like image 753
Amith KK Avatar asked Mar 14 '14 02:03

Amith KK


People also ask

What is pure and impure function in react?

A Pure function is a function that does not modify any external variable. And the Impure function modifies the external variable. A basic principle of functional programming is that it avoids changing the application state and variables outside its scope. Let see how it works in React.

What is an impure function?

An impure function is a function that mutates variables/state/data outside of it's lexical scope, thus deeming it “impure” for this reason. There are many ways to write JavaScript, and thinking in terms of impure/pure functions we can write code that is much easier to reason with.

What is pure and impure function in Python?

Your function is pure if it does not contain any external code. Otherwise, it is impure if it includes one or more side effects.

Which is an example for an impure function?

log() and alert() are impure functions because they have side effects (although they generate the same behavior and always return the same value for identical calls). Any function that changes the internal state of one of its arguments or the value of some external variable is an impure function.


2 Answers

Content taken from this link

Characteristics of Pure Function:

  1. 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.

  2. They do not have any side effects like net­work or data­base calls

  3. They do not mod­ify the argu­ments which are passed to them

Char­ac­ter­isitcs of Impure functions

  1. 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()

  2. They may have any side effects like net­work or data­base calls

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

function impureFunc(value){
  return Math.random() * value;
}

function pureFunc(value){
  return value * value;
}

var impureOutput = [];
for(var i = 0; i < 5; i++){
   impureOutput.push(impureFunc(5));
}

var pureOutput = [];
for(var i = 0; i < 5; i++){
   pureOutput.push(pureFunc(5));
}

console.log("Impure result: " + impureOutput); // result is inconsistent however input is same. 

console.log("Pure result: " + pureOutput); // result is consistent with same input
like image 106
A.T. Avatar answered Oct 12 '22 21:10

A.T.


From Wikipedia - a function may be described as a pure function if both these statements about the function hold:

  1. The function always evaluates the same result value given the same argument value(s). The function result value cannot depend on any hidden information or state that may change as program execution proceeds or between different executions of the program, nor can it depend on any external input from I/O devices.
  2. Evaluation of the result does not cause any semantically observable side effect or output, such as mutation of mutable objects or output to I/O devices.

Therefore, if either statement is false when compared to your code then it is impure.

like image 36
Elliott Frisch Avatar answered Oct 12 '22 22:10

Elliott Frisch