Pass an existing function through function argument

Example:

fun someFunc(n:Int):Int{
   return n
}
fun mainFun(fun:()->(Unit)){
     printnl(fun(4))
//Should print out 4
}
mainFun(someFunc)

Is that possible, and when how?

Yes, you can use function references. Note that type of the function has to match type of the parameter

fun someFun(n: Int): Int {
    return n + 2
} 

fun mainFun(f: (Int)->Int) {
    println(f(4))
} 

fun main() {
    mainFun(::someFun) 
} 
2 Likes

Thank you.