Open In App

How to Pass a Function as a Parameter to Another in Kotlin?

Last Updated : 21 Jun, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In Kotlin, we have the amazing power to work with higher-order functions. These are special kinds of functions that can take other functions as parameters or even return functions as results. This feature makes our code more flexible, cleaner, and easier to understand. In fact, many useful functions in Kotlin’s standard library are higher-order functions for example, map, filter, and let. But before we learn to pass functions around, let’s first understand how to declare and use lambdas, a very simple way to define a function.

Declaring Lambdas

A lambda expression in Kotlin is simply an anonymous function (a function without a name) that we can store in a variable, pass to another function, or return from a function.

Kotlin
val funcMultiply: (Int, Int) -> Int = { a, b -> a * b }
val funcSayHi: (String) -> Unit = { name -> println("Hi, $name!") }

In the above code snippet, funcMultiply is a lambda that takes two Int values and returns an Int and funcSayHi is a lambda that takes a String and returns Unit (which means it returns nothing, just like void in other languages). We can also write simpler lambdas like this.

val sum = { x: Int, y: Int -> x + y }

Notice how the lambda itself contains both the parameters and the logic in a very short and clear way.


Passing a Function (Lambda) into Another Function

So now that we have a general idea of how lambdas work, let's try and pass one in another function-that is, we will try a high-order function. When we pass a function into another function, we are using a higher-order function.

Kotlin
fun operateOnNumbers(a: Int, b: Int, operation: (Int, Int) -> Int): Int {
    return operation(a, b)
}

We can call this function like this:

val result = operateOnNumbers(4, 5, funcMultiply)
println("Result: $result")

Or, even shorter:

val result = operateOnNumbers(4, 5) { x, y -> x + y }
println("Result: $result")

So here, we passed a lambda directly as an argument to another function! This is the main idea behind higher-order functions.

Returning a Function from Another Function

A higher-order function can also return another function. Check out the following code snippet.

Kotlin
fun getDiscountFunction(isSpecialCustomer: Boolean): (Double) -> Double {
    return if (isSpecialCustomer) {
        // 10% discount
        { price -> price * 0.9 } 
    } else {
        // No discount
        { price -> price } 
    }
}

fun main() {
    // calling the above function
    val discount = getDiscountFunction(true)
    println("Discounted price: ${discount(100.0)}")
}

Output:

Discounted price: 90.0

We call getDiscountFunction(true), and it returns a lambda function that applies a 10% discount. We store that returned function in the discount variable. Then we can simply use it as a function: discount(100.0).


Next Article
Article Tags :

Similar Reads