Pure and Impure Methods

Pure and Impure Methods


Pure functions :

Pure functions are functions which will give exact result when the same arguments are passed.

For example the mathematical function sin (0) always results 0. This means that every time you call the function with the same arguments, you will always get the same result. A function can be a pure function provided it should not have any external variable which will alter the behaviour of that variable.

Example:

let square x
return: x * x

Impure functions:


The variables used inside the function may cause side effects though the functions which are not passed with any arguments. In such cases the function is called impure function. When a function depends on variables or functions outside of its definition block, you can never be sure that the function will behave the same every time it’s called. For example the mathematical function random() will give different outputs for the same function call.


let Random number
let a := random()
if a > 10 then
return: a
else
return: 10

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