• +91 9723535972
  • info@interviewmaterial.com

Kotlin Interview Questions and Answers

Question - What are Higher-Order functions in Kotlin?

Answer -

A higher-order function is a function that takes functions as parameters or returns a function. For example, A function can take functions as parameters.

fun passMeFunction(abc: () -> Unit) {
 // I can take function
 // do something here
 // execute the function
 abc()
}
For example, A function can return another function.

fun add(a: Int, b: Int): Int {
 return a + b
}
And, we have a function returnMeAddFunction which takes zero parameters and returns a function of the type ((Int, Int) -> Int).

fun returnMeAddFunction(): ((Int, Int) -> Int) {
 // can do something and return function as well
 // returning function
 return ::add
}
And to call the above function, we can do:

val add = returnMeAddFunction()
val result = add(2, 2)

Comment(S)

Show all Coment

Leave a Comment




NCERT Solutions

 

Share your email for latest updates

Name:
Email:

Our partners