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
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
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
9
20
21Key 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
1. What defines a higher-order function in Kotlin?
2. In `fun operate(x: Int, y: Int, op: (Int, Int) -> Int): Int`, what does the type `(Int, Int) -> Int` describe?
3. Which of these is NOT one of Kotlin's standard scope functions?
4. What programming concept allows a function returned from `multiplier(factor: Int): (Int) -> Int` to remember `factor` after `multiplier` returns?
Was this page helpful?
You May Also Like
Lambda Expressions in Kotlin
Understand how to write and use lambda expressions in Kotlin, including trailing lambda syntax and the implicit it parameter.
Functions in Kotlin
Learn how to declare, call, and simplify functions in Kotlin, including single-expression syntax and the Unit type.
Collection Operations in Kotlin (map, filter, fold)
How to transform, filter, and reduce Kotlin collections using functional-style operations like map, filter, and fold.
Extension Functions in Kotlin
Learn how Kotlin extension functions let you add new behavior to existing classes without modifying or inheriting them.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics