How to Pass a Function as a Parameter to Another in Kotlin?
Last Updated :
21 Jun, 2025
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).
Similar Reads
Passing Variable Arguments to a Function in Kotlin There are a lot of scenarios in which we need to pass variable arguments to a function. In Kotlin, You can pass a variable number of arguments to a function by declaring the function with a vararg parameter. a vararg parameter of type T is internally represented as an array of type T ( Array<T
4 min read
How to Write swap Function in Kotlin using the also Function? Swapping two numbers is one of the most common tasks in programming. Usually, when we swap two values in languages like Java or C++, we either use a third temporary variable or pointers/references to perform the swap. In Java, since we donât have pointers, we almost always rely on a third variable t
3 min read
How to Use try - catch as an Expression in Kotlin? The try statement consists of a try-block, which contains one or more statements. { } must always be used, even for single statements. A catch-block, a finally-block, or both must be present. This gives us three forms for the try statement: try...catchtry...finallytry...catch...finally A catch-block
4 min read
How to Specify Default Values in Kotlin Functions? In Kotlin, you can provide default values to parameters in a function definition. If the function is called with arguments passed, those arguments are used as parameters. However, if the function is called without passing argument(s), default arguments are used. But If you come from the Java world,
4 min read
How to Type Check an Object in Kotlin? Kotlin is a statically typed, general-purpose programming language developed by JetBrains, that has built world-class IDEs like IntelliJ IDEA, PhpStorm, Appcode, etc. It was first introduced by JetBrains in 2011 and is a new language for the JVM. Kotlin is an object-oriented language, and a âbetter
3 min read