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 {' '} 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 {' '} .
Your turn. Fill in the ___ , then run and compare.
Write one higher-order function and reuse it with three different lambdas.
📋 Quick Reference — Higher-Order Functions
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 -> ... }.