Higher-Order Functions & Function Types
A higher-order function is a function that takes another function as a parameter or returns one, treating functions as ordinary values you can pass around, store, and call later.
Learn Higher-Order Functions & Function Types in our free Kotlin course — a beginner-friendly interactive lesson with worked examples, a practice exercise…
Part of the free Kotlin course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
In this lesson you'll learn function types like (Int) -> Int , how to pass and return functions, function references with ::name , trailing-lambda syntax, and the implicit it parameter.
What You'll Learn in This Lesson
1️⃣ Function Types: Functions as Values
In Kotlin, a function has a type just like an Int or a String does. The type is written as (parameterTypes) -> returnType . For example, (Int, Int) -> Int describes any function that takes two Int s and returns an Int . Once a function has a type, you can store it in a variable and call it through that variable.
Notice that add and multiply share the same type, so they are interchangeable as values. A function returning nothing useful has the return type Unit — Kotlin's equivalent of "void".
2️⃣ Passing Functions In (Higher-Order Functions)
A higher-order function accepts a function as a parameter. The parameter is declared with a function type, and inside the body you call it like any other function. When the function parameter is the last argument, Kotlin lets you write the lambda outside the parentheses — the idiomatic trailing-lambda syntax.
When a lambda has exactly one parameter, you can omit it and use the implicit name it . This is why list.filter { it > 0 } reads so cleanly.
3️⃣ Returning Functions & Function References
A higher-order function can also return a function — useful for building configured behaviour like a "multiply by N" factory. And when you already have a named function, the reference operator :: turns it into a value you can pass around, so you don't need to wrap it in a lambda.
Each call to multiplierFor produces a fresh function that "remembers" its factor — a closure. And numbers.filter(::isEven) is cleaner than numbers.filter { isEven(it) } .
Your turn. Fill in the ___ , then run and compare.
Mini-Challenge: A Tiny Calculator
Write one higher-order function and reuse it with three different lambdas.
Common Errors (and the fix)
- ❌ "Type mismatch: expected (Int) -> Int" — Your lambda's parameter or return type doesn't match the declared function type. ✅ Make sure the lambda takes and returns the same types the function type specifies.
- ❌ Calling a function value without parentheses — Writing add alone gives you the function itself, not its result. ✅ Call it with add(2, 3) .
- ❌ Putting () after a reference — ::isEven() is wrong. ✅ A reference is written without parentheses: ::isEven .
- ❌ Using it with two parameters — it only exists for single-parameter lambdas. ✅ Name them: { a, b -> a + b } .
Pro Tips
- 💡 Prefer references: .filter(::isEven) is often clearer than .filter { isEven(it) } .
- 💡 Trailing lambdas read like blocks: repeat(3) { ... } and list.forEach { ... } are both higher-order calls.
- 💡 Name long lambda params: for anything beyond a one-liner, explicit names beat it for readability.
📋 Quick Reference — Higher-Order Functions
Concept
Kotlin Syntax
Function type
(Int, Int) -> Int
Lambda value
{ a, b -> a + b }
Accept a function
fun f(op: (Int) -> Int)
Trailing lambda
f(10) { it + 1 }
Function reference
list.filter(::isEven)
Implicit parameter
{ it * 10 }
Frequently Asked Questions
🎉 Lesson Complete!
- ✅ A function type (Int, Int) -> Int describes a function's shape
- ✅ Functions are values: store them, pass them, return them
- ✅ A higher-order function takes or returns a function
- ✅ Trailing-lambda syntax moves the last lambda outside the parentheses
- ✅ ::name references a named function; it names a single lambda parameter
- ✅ Next lesson: Inline Functions & reified
Practice quiz
What makes a function 'higher-order'?
- It returns an Int
- It takes a function as a parameter or returns one
- It is marked inline
- It has more than three parameters
Answer: It takes a function as a parameter or returns one. A higher-order function takes another function as a parameter, returns one, or both.
What does the function type (Int, Int) -> Int describe?
- A class with two Int fields
- A list of two Ints
- A function taking two Ints and returning an Int
- An Int divided by an Int
Answer: A function taking two Ints and returning an Int. It describes a function taking two Int parameters and returning an Int.
What is the return type of a function that returns nothing useful?
- Unit
- Void
- Null
- Nothing
Answer: Unit. Kotlin uses Unit as the type for functions that return no useful value.
In a single-parameter lambda, what is the implicit parameter name?
- this
- arg
- self
- it
Answer: it. A single-parameter lambda can refer to its argument with the implicit name it.
When is trailing-lambda syntax allowed?
- When the lambda is the last argument
- Only with zero other arguments
- When the lambda is the first argument
- Never in Kotlin
Answer: When the lambda is the last argument. If the function's last parameter is a function, the lambda can move outside the parentheses.
How do you reference an existing named function isEven as a value?
- isEven()
- ::isEven
- &isEven
- isEven.ref
Answer: ::isEven. The :: operator turns a named function into a value, e.g. ::isEven (no parentheses).
What does multiplierFor(3) return in the lesson example?
- The number 3
- A list of multiples
- A new function that multiplies by 3
- An error
Answer: A new function that multiplies by 3. It returns a new function (Int) -> Int that multiplies its input by 3.
What lets a returned lambda remember the factor variable?
- A closure capturing the variable
- A global variable
- The inline keyword
- Garbage collection
Answer: A closure capturing the variable. The lambda forms a closure, capturing factor even after the outer function returns.
Why is ::isEven written without parentheses?
- Parentheses are optional everywhere
- It references the function instead of calling it
- It is a syntax error to omit them
- It only works for Boolean functions
Answer: It references the function instead of calling it. A reference names the function as a value; adding () would call it instead.
Can 'it' be used in a lambda with two parameters?
- Yes, it refers to the first
- Yes, it refers to both
- No, name the parameters explicitly
- Yes, but only inside loops
Answer: No, name the parameters explicitly. it exists only for single-parameter lambdas; with two, name them like { a, b -> ... }.
Continue this course
- Previous: Checkpoint: Kotlin Basics
- Next: Inline Functions & reified