Let's follow these steps to understand the working of high-order functions:
- Let's start by understanding how we declare functions as lambdas:
fun main(args: Array<String>) { val funcMultiply = {a:Int, b:Int -> a*b} println(funcMultiply(4,3)) val funcSayHi = {name: String -> println("Hi $name")} funcSayHi("John")}
- In the preceding code block, we declared two lambdas: one (funcMultiply) that takes two integers and returns an integer, and another (funcSayHi) lambda that takes a string and returns a unit—that is, it returns nothing.
- Although we did not need to declare the type of arguments and the return type in the preceding example, in some cases we need to explicitly declare the argument types and return types. We do this ...