Intermediate 8 min readKotlin 2.0
Kotlin Higher-Order Functions — Passing Functions as Parameters
A higher-order function accepts a function as a parameter or returns a function. This is central to Kotlin's functional programming style.
What You Will Learn
- What a function type is
- Pass a function as a parameter
- Use lambda syntax at the call site
- Return a function from a function
- Understand the trailing lambda convention
Function Types
A function type describes a function's signature. (Int, Int) -> Int means: takes two Ints, returns an Int.
Accepting a Function
kotlin
fun calculate(a: Int, b: Int, operation: (Int, Int) -> Int): Int {
return operation(a, b)
}
fun main() {
val result1 = calculate(10, 5) { x, y -> x + y }
val result2 = calculate(10, 5) { x, y -> x * y }
println(result1)
println(result2)
}Output
15
50
operation is a parameter with function type (Int, Int) -> Int. At the call site, we pass a lambda { x, y -> x + y }. The trailing lambda syntax places the lambda outside the parentheses.
Beginner Tip: When the last parameter is a function, you can pass it as a trailing lambda outside the (): calculate(10, 5) { ... }
Returning a Function
A function can return another function.
Function Returning a Function
kotlin
fun multiplier(factor: Int): (Int) -> Int {
return { number -> number * factor }
}
fun main() {
val triple = multiplier(3)
val double = multiplier(2)
println(triple(7))
println(double(7))
}Output
21
14
multiplier returns a lambda that captures factor from its outer scope. triple and double are now functions you can call.
Practice Exercise
Exercisepredict output
What prints? fun apply(n: Int, f: (Int) -> Int) = f(n) fun main() { println(apply(5) { it * it }) }
Quick Quiz
Quick Quiz
What is a higher-order function?
Frequently Asked Questions
Related Tutorials
Last updated: 2026-05-01Kotlin 2.0
Written by KotlinGuide Editorial Team · Reviewed by KotlinGuide Technical Review