Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the responsive-lightbox domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /var/www/wp-includes/functions.php on line 6114

Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the hueman domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /var/www/wp-includes/functions.php on line 6114
Scala - Nested Function - Testingpool

Scala – Nested Function

In Scala, a function defined inside another function is called nested function or local function. This feature is not supported by Java. In other languages, we can call a function from another function but that is not nested function. Multiple function definitions can be defined inside parent function.Let’s understand this with examples.

Example1:

A function named calculation has been written and inside that multiple nested functions have been created as shown below. These nested function will do different calculations and print the result.

object nestedFunc {

  def main(args: Array[String]): Unit = {
    //calling Function
    calculate(3,2)
  }
  //Function
  def calculate(a:Int,b:Int): Unit ={
    //Nested Function
    def add(): Unit ={
      println("Addition of a and b is " + (a + b))
    }
    //Nested Function
    def mul(): Unit ={
      println("Multiplication of a and b is " + (a * b))
    }
    //Nested Function
    def sub(): Unit ={
      println("Subtraction of a and b is " + (a - b))
    }
    add()
    mul()
    sub()
  }
}
Output:
Addition of a and b is 5
Multiplication of a and b is 6
Subtraction of a and b is 1

A nested function can be defined inside another function and so on. It is called nesting of function. Theoretically, nesting is possible to unlimited depth but practically it id done to few levels.Let’s see an example below where nesting is done till 3rd level.

Example2:

object nestedFunc {

  def main(args: Array[String]): Unit = {
    //calling Function
    CarDetails()
  }
  //Function
  def CarDetails(): Unit ={
    model()
    //first Nested Function
    def model(): Unit ={
      println("What is the model of the car?")
        color()
        //Second Nested Function
        def color(): Unit ={
          println("What is the color of car?")
          price()
            //Third Nested Function
            def price(): Unit ={
              println("What is price of the car?")
            }
        }
    }
  }
}
Output:
What is the model of the car?
What is the color of car?
What is price of the car?

Example3:

In the below example, function is doing factorial of given number. A nested function named fact is defined inside parent function named factorial.

object NestedFunc2 {

  def main(args: Array[String]): Unit = {
    println("factorial of 3 is " + factorial(3));
    println("factorial of 5 is " + factorial(5));
  }

  def factorial(x: Int): Int = {
    def fact(x: Int, accumulator: Int): Int = {
      if (x <= 1) accumulator
      else fact(x - 1, x * accumulator)
    }
    fact(x, 1)
  }
}
Output:
factorial of 3 is 6
factorial of 5 is 120
Avatar photo

Shekhar Sharma

Shekhar Sharma is founder of testingpool.com. This website is his window to the world. He believes that ,"Knowledge increases by sharing but not by saving".

You may also like...