Open In App

Kotlin Higher-Order Functions

Last Updated : 18 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Kotlin language has superb support for functional programming. Kotlin functions can be stored in variables and data structures, passed as arguments to and returned from other higher-order functions. 

Higher-Order Function

In Kotlin, a function that can accept a function as a parameter or return a function is called a Higher-Order function. Instead of Integer, String, or Array as a parameter to a function, we will pass an anonymous function or lambda. Frequently, lambdas are passed as parameters in Kotlin functions for convenience. 


Passing a lambda expression as a parameter to a Higher-Order Function

We can pass a lambda expression as a parameter to a Higher-Order Function. 
There are two types of lambda expressions that can

  • Lambda expression that returns Unit
  • Lambda expression that returns any of the values integer, string, etc.

Kotlin program of a lambda expression that returns Unit

Kotlin
// lambda expression
var lambda = {println("GeeksforGeeks: A Computer Science portal for Geeks") }

// higher-order function 
fun higherfunc( lmbd: () -> Unit ) {     // accepting lambda as parameter
    lmbd()                               //invokes lambda expression
}
fun main(args: Array<String>) {
     //invoke higher-order function
    higherfunc(lambda)                 // passing lambda as parameter
}


Output: 

GeeksforGeeks: A Computer Science portal for Geeks

Explanation: 

Let's understand the above program step by step:
At the top, we define a lambda expression that contains print() to print a string to the standard output. 

var lambda = {println("GeeksforGeeks: A Computer Science portal for Geeks") } 

Then, we define a higher-order function that contains one parameter. 

lmbd: () -> Unit

lmbd is the local name for the receiving lambda parameter. 
() represents that the function does not accept any arguments. 
Unit represents that the function does not return any value.
In the main function, we have invoked the higher-order function by passing the lambda expression as a parameter. 

higherfunc(lambda)

Kotlin program of a lambda expression that returns an Integer value

Kotlin
// lambda expression
var lambda = {a: Int , b: Int -> a + b }

// higher order function
fun higherfunc( lmbd: (Int, Int) -> Int) {      // accepting lambda as parameter
        
    var result = lmbd(2,4)    // invokes the lambda expression by passing parameters                    
    println("The sum of two numbers is: $result")
}

fun main(args: Array<String>) {
    higherfunc(lambda)           //passing lambda as parameter
}


Output: 

The sum of two numbers is: 6

Explanation: 

Let's understand the above program step by step:
At the top, we define a lambda expression defined which returns an Integer value. 

var lambda = {a: Int , b: Int -> a + b }

Then, we have defined a higher-order function that accepts the lambda expression as a parameter.  

lmbd: (Int, Int) -> Int


lmbd is the local name for the receiving lambda parameter. 
(Int,Int) represents that the function accepts two integer-type parameters. 
-> Int represents that the function returns an integer value.
In the main function, we have invoked the higher-order function by passing the lambda as a parameter. 

higherfunc(lambda)

Passing a function as a parameter to a Higher-Order function

We can pass a function as a parameter in a Higher-Order function. 
There are two types of functions that can be passed- 

  • The function that returns Unit
  • The function that returns any of the values integer, string, etc

Kotlin program passes a function that returns Unit

Kotlin
    // regular function definition
fun printMe(s:String): Unit{
    println(s)
}
   // higher-order function definition
fun higherfunc( str : String, myfunc: (String) -> Unit){
   // invoke regular function using local name
    myfunc(str)
}
fun main(args: Array<String>) {
    // invoke higher-order function
    higherfunc("GeeksforGeeks: A Computer Science portal for Geeks",::printMe)
}


Output: 

GeeksforGeeks: A Computer Science portal for Geeks

Explanation: 

At the top, we define a regular function printMe() which accepts a parameter of String type and returns Unit. 

fun printMe(s:String): Unit


(s: String) is the only parameter 
Unit represents the return type
Then, we define the Higher-order function as 

fun higherfunc( str : String, myfunc: (String) -> Unit)


It receives two parameters, one of String type and another one is a function 

str: String represents a string parameter 
myfunc: (String) -> Unit represents that it accepts a function as a parameter, which returns Unit.

From the main function, the higher function is invoked by passing the string and the function as arguments. 

 higherfunc("GeeksforGeeks: A Computer Science portal for Geeks",::printMe)

Kotlin program passes a function that returns an integer value

Kotlin
    // regular function definition
fun add(a: Int, b: Int): Int{
    var sum = a + b
    return sum
}
    // higher-order function definition
fun higherfunc(addfunc:(Int,Int)-> Int){
    // invoke regular function using local name
    var result = addfunc(3,6)
    print("The sum of two numbers is: $result")
}
fun main(args: Array<String>) {
    // invoke higher-order function
    higherfunc(::add)
}


Output: 

The sum of two numbers is: 9

Explanation: 

At the top, we define the regular function as 

fun add(a: Int, b: Int): Int{
var sum = a + b
return sum
}


It accepts two parameters of Integer type, and returns the sum of both integers. Then, we define the higher-order function as 

fun higherfunc(addfunc:(Int,Int)-> Int)


It accepts a function that contains two parameters and 
calls the regular function addfunc(3,6) by passing the parameters.
From the main function, we invoke the higher-order function by passing the regular function as a parameter 

higherfunc(::add)

Returning a function from a Higher-Order function

We can return a function from a higher-order function. While returning the function, we have to specify the parameter types and return type of the regular function in the return type of the higher-order function. 

Kotlin program of a function returning another function

Kotlin
      // function declaration
fun mul(a: Int, b: Int): Int{
    return a*b
}
    //higher-order function declaration
fun higherfunc() : ((Int,Int)-> Int){
    return ::mul
}
fun main(args: Array<String>) {
     // invoke function and store the returned function into a variable 
    val multiply = higherfunc()  
    // invokes the mul() function by passing arguments
    val result = multiply(2,4)   
    println("The multiplication of two numbers is: $result")
}


Output: 

The multiplication of two numbers is: 8

Explanation: 

In the top of program we define mul() function which accepts two parameters and its return type is also an integer. 

fun mul(a: Int, b: Int): Int

Then, we define the higher-order function having return type as a function. 

fun higherfunc5() : ((Int,Int)-> Int){
return ::mul
}

::mul represents that it return mul() function 
(Int,Int) represents that mul accepts two integer-type parameters 
Int represents that mul returns an integer value.
In main function, we have called the higher function which returns another function and store this in a variable multiply . 

val multiply = higherfunc()

Then we invoke the mul() function using the local variable multiply(2,4) by passing two arguments. 


Next Article
Article Tags :

Similar Reads