← Back to Kotlin Course | Chapter 9: Lambdas & Higher-Order Functions | Lesson 5 of 6

Function References

A function reference lets you point at an already-existing function by name and hand it over as if it were a value, using two colons.

Referencing a Top-Level Function

::functionName refers to an existing standalone function, letting it be passed directly wherever a matching function type is expected, without wrapping it in a lambda.

Example: Referencing a Top-Level Function

markup
fun isEven(n: Int): Boolean = n % 2 == 0

fun main() {
    val numbers = listOf(1, 2, 3, 4, 5, 6)
    val evens = numbers.filter(::isEven)
    println(evens)
}

Referencing a Member Function on an Instance

instance::method creates a bound reference to a member function tied to that specific object, usable directly as a function value.

Example: Referencing a Member Function on an Instance

markup
class Greeter(val greeting: String) {
    fun greet(name: String) = "$greeting, $name!"
}

fun main() {
    val greeter = Greeter("Hi")
    val greetFn: (String) -> String = greeter::greet
    println(greetFn("Kotlin"))
}

Referencing a Member Function on a Class

ClassName::method creates an unbound reference, where the receiver instance must be supplied as the first argument when the reference is actually called.

Example: Referencing a Member Function on a Class

markup
data class Person(val name: String) {
    fun shout() = name.toUpperCase()
}

fun main() {
    val shoutFn: (Person) -> String = Person::shout
    println(shoutFn(Person("ana")))
}

Constructor References

::ClassName creates a reference to a class's constructor, which can be used just like a function that builds new instances.

Example: Constructor References

markup
data class Point(val x: Int, val y: Int)

fun main() {
    val coordinates = listOf(1 to 2, 3 to 4)
    val points = coordinates.map { (x, y) -> Point(x, y) }
    println(points)
}
Common Mistakes
  1. Forgetting the :: syntax and trying to pass a function's name alone, which Kotlin interprets as trying to call it rather than reference it.
  2. Using a function reference where the referenced function's signature doesn't actually match what the receiving parameter expects.
  3. Not realizing member function references need either a bound receiver (instance::method) or an unbound one (ClassName::method) depending on how they'll be called.
Chapter Summary
  • ::functionName creates a reference to an existing top-level or member function, which can be passed wherever a matching function type is expected.
  • ClassName::method creates an unbound reference, expecting the receiver as the first parameter when called.
  • instance::method creates a bound reference tied to that specific instance.
  • Function references are useful for passing existing functions directly to higher-order functions instead of wrapping them in a lambda.
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.