Updated 2 April 2018
In this blog we are going to cover higher-order functions in kotlin with example. Higher order function can be passed as a parameter or can be returned from a function, the function which does the same is known as a higher-order function. In other words, a higher-order function is a function that takes functions as parameters or returns a function.
A higher-order function is a function that can take other functions as parameters and returns other functions.
You can create a higher order function like any other function in Kotlin, but you need to create an parameter that has a function as the type.
Let’s take an example:
1 2 3 4 5 6 7 |
val repeat: String.(Int) -> String = { times -> repeat(times) } val twoParameters: (String, Int) -> String = repeat // OK fun highOrderFunctionCall(f: (String, Int) -> String): String { return f("hello", 3) } val result = highOrderFunctionCall(twoParameters) |
OUTPUT: hellohellohello
Here, highOrderFunctionCall() is a function which take twoParameters() function as parameter.
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.