GeeksforGeeks App
Open App
Browser
Continue

# Pure Function In Scala

In any programming language, there are two types of functions:

1. PURE FUNCTIONS

2. IMPURE FUNCTIONS
There is a basic difference between the two, that is pure function doesn’t change the variable it’s passed and an impure function does. For example, there exists a function which increases the input by 10. So, there are two possibilities, the first one is it could directly return x + 10 and so x does not change whereas in another case it could go with the statement like x = x + 10 and then return x, thus changing the value of x. So, the first function is pure whereas the other is impure.

## Pure Function in scala

A function is called pure function if it always returns the same result for same argument values and it has no side effects like modifying an argument (or global variable) or outputting something.
They are those functions which don’t read any other values except those given as input and follows its internal algorithm to produce the output. A pure function is side-effect free i.e it doesn’t change the value of the variable implicitly and thus doesn’t end up altering the values of the input.
These Scala String methods are also pure functions:

• isEmpty
• length
• substring

A pure function can be made as shown in the following program code:
Example 1:

## Scala

 `// Pure function In Scala``object` `GFG``{``    ``// Driver code``    ``def` `main(args``:` `Array[String])``    ``{``        ``def` `square(a``:``Int) ``=` `{``            ``var` `b``:``Int ``=` `a * a;``            ``println(``"Square of the number is "` `+ b);``            ``println(``"Number is "` `+ a);``        ``}``        ``square(``4``);``    ``}``}`

Output:

```Square of the number is 16
Number is 4```

Now, we know that this function don’t change the variable implicitly so we add the result of two different functions in different ways, as shown:
Example 2:

## Scala

 `// Scala Pure Function``object` `GFG``{``    ``// Driver code``    ``def` `main(args``:` `Array[String])``    ``{``        ``// Function 1``        ``def` `add(a``:``Int, b``:``Int)``:``Int ``=` `{``            ``var` `c``:``Int ``=` `a + b;``            ``return` `c;``        ``}``        ` `        ``// Function 2``        ``def` `multiply(a``:``Int, b``:``Int)``:``Int ``=` `{``            ``var` `c``:``Int ``=` `a * b;``            ``return` `c;``        ``}``        ` `        ``// Calculating same value but changing``        ``// the position of the functions``        ``println(``"Output in case 1 :"` `+``                ``add(``1``, ``2``) * multiply(``3``, ``4``));``        ``println(``"Output in case 2 :"` `+``                ``multiply(``4``, ``3``) * add(``2``, ``1``));``    ``}``}`

Output:

```Output in case 1 :36
Output in case 2 :36```

Now, as output of both the cases are same then it is proved that input in the pure function in scala is not changed implicitly.
Below are befits Of using Pure Functions:

1. They don’t cause any implicitly changes in input.
2. They are easier to test.
3. They can be debug easily.

My Personal Notes arrow_drop_up