A higher-order function in Kotlin will accept another function as its parameter and then returns that function to the caller.
The higher-order function in the below example will accept two parameters and another function which will get returned to the caller and then returns the result of the power of a number to its caller.
fun main(args: Array<String>) {
val result = powerOfCalculator(5, 3, ::powerOf)
println(result)
}
fun powerOf(base : Int, exponent : Int) = Math.pow(base.toDouble(), exponent.toDouble())
fun powerOfCalculator(base : Int, exponent : Int, operation : (Int, Int) -> Double) : Double {
return operation(base, exponent)
}
The above Kotlin program will display the below outcome:-
125.0