Julia
Julia

Reputation: 369

How to call a function with a parameter in another function in kotlin

I have function which returns some value. In the function parameter I pass the same value:

 fun getValue (value:String):String {
        var message = value
        value = "Hello"
        return message
    }

How can I call getValue function in another function? For example:

fun getResult (){
var a = getValue (what here?)
}

Upvotes: 0

Views: 2470

Answers (1)

Reyhane Farshbaf
Reyhane Farshbaf

Reputation: 553

You can pass a function as a prameter for another function in kotlin. Kotlin functions can take other functions in arguments, or even return them.

fun getResult (func:(String) -> String) {
        //some code
        var a = func("some string")
    }

    fun getValue (value:String):String {
        var message = value
        return message
    }

and call getResult and pass function to it:

getResult({  getValue("Hello") })

Upvotes: 1

Related Questions