Scala, the better Java

one stop site for scala learners

Category Archives: local function

Local/Inner function in Scala

In Scala, we can define function inside function. In other words, inner/local function definition is allowed in Scala. The same concept is not possible in Java. In Java, you can define functions only at the class level. But, When it comes to defining function inside function, it is quite possible in Scala. Inner/local function is some way complements Functional programming in Scala.

Lets starts with simple demo snippets.

Lets define a Scala object called MyFactorial.scala. The code sample simply creates Singleton object names MyFactorial. I have defined main function as well so that we can simple call, MyFactorial.main(Array(0))

object MyFactorial {

  // main function
  def main(args: Array[String]) :Unit = {
    println("hello factorial!");
  }

}

Now, proceeding further, lets add factorial function called fact() which accepts an integer to calculate its corresponding factorial value.

 

See below code snippet which performs factorial of a given integer with the use of local/inner function named perform.

object MyFactorial {

// main function
def main(args: Array[String]) :Unit = {
  println("hello factorial!");
  val n = 5;

  // string interpolation
  val msg = s"The factorial of $n is ${fact(n)}.";
  println(msg);
}

 // method which calculates factorial
 def fact(n: Int):Int = {

 	// inner or local function
 	def perform(n:Int, result:Int):Int = {
 		if( n <= 0 ) {
 			return result;
 		} else {
 			return perform( n-1, n* result);
 		}
 	}

 // call to inner function
 return perform(n, 1);
 }

}

As you can see from the above code snippet that, function fact() has defined inner/local function, perform() to perform actual calculation on given number and return final result.

I am from Java programming background, as you can see, I have written fully expressed code snippet with all the curly braces in if-else statement, semicolon after every statement.

But in Scala, programming language, we can write very compact code as shown in below version of same piece of code snippet without

  • return keywords
  • curly braces for if-else statement
  • optional semicolons

 

object MyFactorial {

  // main function
  def main(args: Array[String]) :Unit = {
    println("hello factorial!")

    val n = 5

    // string interpolation
    val msg = s"The factorial of $n is ${fact(n)}."
    println(msg)
  }

  // method which calculates factorial
  def fact(n: Int):Int = {

    // inner or local function
    def perform(n:Int, result:Int):Int = {
      if( n <= 0 ) result
      else perform( n-1, n* result)
    }

  // call to inner function
  perform(n, 1)
  }

}

 

I am printing output message using another Scala feature related to String processing called String Interpolation as shown in above code snippet inside main() function.

The output is shown below,

 hello factorial! The factorial of 5 is 120. 

 

On contrary, we cannot use local/inner function in Java.

I hope you enjoyed this post.