Signup/Sign In

Kotlin Higher Order Function

In the previous tutorial we discussed about the Kotlin lambda functions. In this tutorial we will move one step ahead and discuss about higher order functions in Kotlin.

Kotlin considers its functions as a first-class citizen. It allows a function to be used as a variable. So, a function can be passed as an argument to another function, returned from a function, stored in a variable or data structure, etc.

Kotlin Higher order function

A function which takes another function as an argument or returns a function is known as a higher order function. Often lambda expressions are passed as an argument to a higher order function or returned from it. An anonymous function can be used for the same also.

We'll see few examples in which we'll pass functions and lambdas as argument or return them from another function.

Kotlin Function as an argument

In this example, the function sayHello() is passed as an argument to higherOrderFunction() and it is called from the main() function:

fun sayHello(name: String){
    println("In sayHello() function")
    println("Say hello to $name")
}

fun higherOrderFunction(functionName: (name: String)-> Unit, name: String){
    println("In higher order function")
    println("Calling sayHello() function...")
    functionName(name)
}

fun main() {
    higherOrderFunction(::sayHello, "Ninja")
}


In higher order function
Calling sayHello() function...
In sayHello() function
Say hello to Ninja

What happened in the Code above:

  • The sayHello() function simply prints two strings.

  • The higherOrderFunction() function takes two arguments. First argument is a function and second is a string. To accept a function as an argument, we need to define its definition in the form of lambda expression in argument. Let us see its subparts:

    • The name given to function taken as an argument is functionName. So we'll call the function accepted ( i.e. sayHello()) with functionName() now onwards.

    • Next we added (name: String) which reflects the arguments of sayHello() function.

    • After -> we mentioned the return type of sayHello() function.

  • Inside higherOrderFunction() function we call functionName() (which is sayHello()) and passed name as the argument.

  • To pass a function as an argument we use :: operator. We called higherOrderFunction() function from main() and passed sayHello() as an argument.

Kotlin Function as a return value

In this example, we will return sayHello() function from higherOrderFunction() function.

fun sayHello(name: String){
    println("In sayHello() function")
    println("Say hello to $name")
}

fun higherOrderFunction(): (name:String) -> Unit{
    println("In higher order function")
    // return the sayHello function
    return ::sayHello
}

fun main() {
    val functionName = higherOrderFunction()
    functionName("Ninja")
}


In higher order function
In sayHello() function
Say hello to Ninja

Code Explanation:

  • In main() function, the higherOrderFunction() is called and the returned value is stored in functionName variable.

  • In higherOrderFunction() function, the return type is mentioned as (name:String) -> Unit. It represents the arguments and return type of sayHello() function(which is the function to be returned).

  • The sayHello() function is returned using :: operator.

  • The returned function is stored in functionName variable. It is called using functionName("Ninja") which is same as calling sayHello("Ninja").

Kotlin Lambda as an argument

Most of the times, instead of a function we pass a lambda expression as an argument or return it. Let us see the same examples we discussed about but we'll use lambdas this time.

fun higherOrderFunction(functionName: (name: String)->Unit, name: String){
    println("In higher order function")
    println("Calling received function...")
    functionName(name)
}

fun main() {
    higherOrderFunction({ name: String ->
        println("Inside the lambda function")
        println("Say hello to $name")
    }, "Ninja")
}


In higher order function
Calling received function...
Inside the lambda function
Say hello to Ninja

In this example, instead of creating a function sayHello() separately and then passing its name as an argument we created a lambda expression and passed it directly to our high order function. It works in the same way as explained in case of function as an argument.

Kotlin Lambda as a return value

Finally, let's look at an example in which the lambda is returned from a function:

fun higherOrderFunction(): (name:String) -> Unit{
    println("In higher order function")
    return {name ->
        println("Inside the lambda function")
        println("Say hello to $name")}
}
fun main() {
    val functionName = higherOrderFunction()
    functionName("Ninja")
}


In higher order function
Inside the lambda function
Say hello to Ninja

Summary

In this tutorial we discussed about Kotlin higher order functions with multiple examples. In the next tutorial we'll discuss about Inline functions in Kotlin.



About the author:
I'm a writer at studytonight.com.