Pure Function in Scala

Scala | Pure Function: Here, we are going to learn the concept of pure function with its usages, syntax and examples.
Submitted by Shivang Yadav, on July 16, 2020

A pure function is a function that does not have a dependency on i/o and other values that are out of the function's scope. And depending only on the values that are passed in parameters of the function.

Let's elaborate the function more,

A pure function is Scala depends on:

  1. Parameters passed to it.
  2. The algorithm (operations) of the function.

A pure function is independent on:

  1. Any external I/O like input peripherals, database, files, services, etc. Also, it cannot provide any output to any output.
  2. It cannot use any values that are outside the function’s scope. Also, no variables can be modified outside its scope.

As defined by Alvin Alexander, pure function = Output depends on Input + No side effects

Some examples of pure function are:

  1. isEmpty()
  2. sqrt()

Now, let's create a pure function:

Example: Mileage Calculator

object MyObject {
    def mileageCal(distance:Int, fuel:Int): Float = {
        val mileage = distance/fuel
        return (mileage);
    }
    def main(args: Array[String]) {
    println("The mileage of my bike is: " + mileageCal(342, 9))
    }
}

Output:

The mileage of my bike is: 38.0

In the above code, we have created a function named mileageCal() which accepts two integer values which are distance and fuel. The function will find mileage using the formula mileage = distance/fuel. And the function returns this value back to the calling function.

Impure Functions in Scala

All the functions that are not pure are impure. This makes the definition of an impure function as "A function that is dependent on i/o or other values out of function's scope along with parameter values."

Now, let's create an impure function:

Example: Constant multiplier

object MyObject {
    val constant = 12.4;
    def constantMul(value:Int): Double = {
        val result = value*constant; 
        return (result);
    }
    
    def main(args: Array[String]) {
        println("The result of constant multiplier is: " + constantMul(34))
    }
}

Output:

The result of constant multiplier is: 421.6

In the above code, we have created a constantMul() function which accepts a parameter value that is multiplied by a value defined in the object i.e. dependent on a value outside the scope of the function.

Benefits of using a pure function

Some benefits of using a pure function in Scala:

  1. Easily combined: Pure functions are I/O independent, hence they can be easily combined with other functions.
  2. Easy to test and debug: Due to their independency on outer values, their test and debugging is easy as you don't need to look outside the function's scope.
  3. They don't change input values and other values outside their scope.





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.