Computer Science, asked by PINU4205, 1 year ago

Difference between pure and impure procedures in system programming

Answers

Answered by Sushil2004
3
These are functions which are sure to provide exact result when the same arguments are passed. For having a function as pure function if must not have any external variable, data store call, ajax request or any other global variables.

This means that you can be completely sure that every time you call the function with the same arguments, you will always get the same result.

Example:

function square(x){ return x*x }

The above function is pure function as we are always certain what will be result and there will be no different results for same input.

Impure Functions:

When function uses any variables not passed in as arguments, the variables used inside functions may cause side effects. Lets say in case of ajax calls it will fail, In such cases the function is called impure function. When a function depends on variables or functions outside of its lexical scope, you can never be sure that the function will behave the same every time it’s called. Following are impure functions:

function getRandom(number) { a = Math.random() if(a > 10){ return a } else{ return 10 } }

Here the function getRandom is impure as it is not sure what will be the result when we call the function.

So this is the difference between pure and impure functions.



 

 


Answered by laraibmukhtar55
1

Pure and impure procedure in system programming:

• Pure procedures, which are also contained in Fortran 95, are prefixed by the pure keyword in the same way as recursive process are prefixed by the recursive keyword.  

• They must be side-effect free in the sense that they cannot alter the global state of the program.

• An impure procedure is a user-defined process that has side effects.

Hope it helped.....

Similar questions