The modified sum function will be written in such a way that it only takes a single parameter, a function f. As before, the function parameter f takes a parameter of type Int and returns a value of type Int. However, this time around, sum isn’t returning an Int type value, rather it is returning a complete function. Let’s look at the code below.
xxxxxxxxxx
def sum(f: Int => Int): (Int,Int) => Int ={
def sumHelper(a: Int, b: Int): Int =
if(a>b) 0
else f(a) + sumHelper(a+1, b)
sumHelper
}