scala

Scala Higher Order Function

Higher order function is a function that either takes a function as an argument or returns a function. In other words, we can say a function which works with function is called higher-order function.

Higher order function allows you to create

  • function composition
  • lambda function
  • anonymous function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
object methodEx {
 
object Higher_order {
 
                    //25       multiplyBy2
  def functionExample(a: Int, f: Int => AnyVal): Unit = {
    println(f(a))                       // Calling that function   
  }// multiplyBy2(25)
                 //25
  def multiplyBy2(a: Int): Int = {
    a * 2
  }// 25 * 2
 
  def main(args: Array[String]) = {
 
    functionExample(25, multiplyBy2)    // Passing a function as parameter  
  }

For video click my YouTube channel

Leave a Comment