100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Kotlin

Higher-Order Functions in Kotlin

Learn how Kotlin functions can accept or return other functions, enabling flexible, reusable, functional-style code.

Functions & LambdasIntermediate9 min readJul 8, 2026
Analogies

Introduction

A higher-order function is a function that either takes one or more functions as parameters, returns a function, or both. This is possible because Kotlin treats functions as first-class citizens: they can be stored in variables, passed around, and invoked like any other value. Higher-order functions are the foundation of Kotlin's functional programming features and power much of its standard library, including collection operations and scope functions.

🏏

Cricket analogy: A captain who can hand the ball (a function) to any bowler and later swap strategies mid-over treats bowling plans as first-class objects, just as Kotlin lets you store, pass, and invoke functions like variables, powering operations like filtering the best economy-rate bowlers from a squad list.

Syntax

kotlin
fun operate(x: Int, y: Int, op: (Int, Int) -> Int): Int {
    return op(x, y)
}

// A function that returns a function
fun multiplier(factor: Int): (Int) -> Int {
    return { number -> number * factor }
}

Explanation

In operate, the parameter op has the function type (Int, Int) -> Int, meaning it accepts a lambda or function reference that takes two Ints and returns an Int. Inside operate, calling op(x, y) invokes whatever function was passed in. The multiplier function demonstrates the reverse: it returns a new function (a lambda) that remembers the factor value from its enclosing scope, an example of a closure. The Kotlin standard library uses this pattern heavily through scope functions such as let, apply, run, also, and with, each of which is itself a higher-order function that takes a lambda operating on a receiver object.

🏏

Cricket analogy: A matchup(bowler, batsman) -> outcome strategy slot accepts any two-input, one-output plan the coach passes in, and just as a scouting report can remember a specific opponent's weakness even after the meeting ends (a closure over factor), Kotlin's scope function let applies a block to a player's stats and returns the result.

Example

kotlin
fun operate(x: Int, y: Int, op: (Int, Int) -> Int): Int {
    return op(x, y)
}

fun main() {
    val sum = operate(4, 5) { a, b -> a + b }
    val product = operate(4, 5) { a, b -> a * b }
    println(sum)
    println(product)

    val triple = multiplier(3)
    println(triple(7))
}

fun multiplier(factor: Int): (Int) -> Int = { number -> number * factor }

Output

kotlin
9
20
21

Key Takeaways

  • A higher-order function takes a function as a parameter, returns one, or both.
  • Function parameters are typed using function types like (Int, Int) -> Int.
  • Functions returned from other functions can close over surrounding variables (closures).
  • Standard library scope functions (let, apply, run, also, with) are higher-order functions.
  • Higher-order functions enable flexible, reusable code without duplicating logic for each operation.

Practice what you learned

Was this page helpful?

Topics covered

#Kotlin#KotlinProgrammingStudyNotes#Programming#HigherOrderFunctionsInKotlin#Higher#Order#Functions#Syntax#StudyNotes#SkillVeris