Tamil Nadu Board of Secondary EducationHSC Science Class 12th

# Explain with example Pure and impure functions. - Computer Science

Explain with example Pure and impure functions.

#### Solution

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 in 0.
Let us see an example.
let square x
return: x * x
• The above function square is a pure function because it will not give different results for the same input.

Impure functions:

• The variables used inside the function may cause side effects through the functions which are not passed with any arguments. In such cases, the function is called the impure function.
• When a function depends on variables or functions outside of its definition block, we can never be sure that the function will behave the same every time it’s called.
• For example, the mathematical functions 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.
Concept: Pure Functions
Is there an error in this question or solution?

Share