Higher-Order Functions in Kotlin

From the Kotlin cheat sheet · Functions · verified Jul 2026

Higher-Order Functions

Functions as parameters and lambdas

kotlin
// Lambda expressions
val sum = { x: Int, y: Int -> x + y }
val square: (Int) -> Int = { it * it }

// Higher-order function
fun calculate(x: Int, y: Int, operation: (Int, Int) -> Int): Int {
    return operation(x, y)
}

val result = calculate(10, 5) { a, b -> a * b }

// Function with receiver
fun buildString(action: StringBuilder.() -> Unit): String {
    val sb = StringBuilder()
    sb.action()
    return sb.toString()
}

val html = buildString {
    append("<h1>")
    append("Title")
    append("</h1>")
}

// Inline functions
inline fun measureTime(block: () -> Unit): Long {
    val start = System.currentTimeMillis()
    block()
    return System.currentTimeMillis() - start
}
💡 Lambdas are functions defined with { } syntax
⚡ inline functions reduce overhead by inlining at call site
📌 crossinline prevents non-local returns in lambdas
🟢 Function with receiver enables DSL-style APIs

More Kotlin tasks

Back to the full Kotlin cheat sheet