def operateOnNumbers(a: Int, b: Int, operation: (Int, Int) => Int): Int = {
operation(a, b)
}
val add: (Int, Int) => Int = (a, b) => a + b
val subtract: (Int, Int) => Int = (a, b) => a - b
val result1 = operateOnNumbers(5, 3, add) // Passes the 'add' function as an argument
val result2 = operateOnNumbers(7, 2, subtract) // Passes the 'subtract' function as an argument
println(result1)
println(result2) ​
Output :
8 //result1
5 //result2​
operateOnNumbers
` is a higher-order function that takes two integer values `a
` and `b
`, along with an operation function as arguments. The operation function is defined as `(Int, Int) => Int
`, meaning it takes two integers and returns an integer.operateOnNumbers
` function applies the operation function to the provided arguments `a
` and `b
` and returns the result. The `add
` and `subtract
` functions are defined as separate functions that match the required `(Int, Int) => Int
` function signature.add
` and `subtract
`) to the `operateOnNumbers` higher-order function, we can perform different operations on the input numbers.