Kotlin: how to pass a function as parameter to another?

Given function foo :

fun foo(m: String, bar: (m: String) -> Unit) {
    bar(m)
}

We can do:

foo("a message", { println("this is a message: $it") } )
//or 
foo("a message")  { println("this is a message: $it") }

Now, lets say we have the following function:

fun buz(m: String) {
   println("another message: $m")
}

Is there a way I can pass “buz” as a parameter to “foo” ?
Something like:

foo("a message", buz)

11 Answers
11

Leave a Comment