Function references let you use a function as a value. This helps when you want to pass a function to another function or store it for later.
Function references (::functionName) in Kotlin
val reference = ::functionNameThe double colon (::) is used before the function name to get its reference.
You can use this reference to call the function or pass it around.
fun greet() { println("Hello!") } val greetRef = ::greet greetRef()
fun square(x: Int) = x * x val squareRef: (Int) -> Int = ::square println(squareRef(5))
fun square(x: Int) = x * x val numbers = listOf(1, 2, 3) val doubled = numbers.map(::square) println(doubled)
This program stores a reference to the greet function and calls it through the reference.
fun greet() { println("Hello, Kotlin!") } fun main() { val greetFunction = ::greet greetFunction() }
Function references can refer to top-level functions, member functions, or constructors.
When using member function references, you may need an instance to call the function.
Function references improve code clarity by avoiding verbose lambda expressions.
Function references use :: before a function name to treat it as a value.
They let you pass or store functions easily.
They work well with collections and higher-order functions.
Practice
::functionName represent?Solution
Step 1: Understand the :: operator in Kotlin
The::operator is used to get a reference to a function, not to call it.Step 2: Differentiate between calling and referencing
Calling a function uses parentheses likefunctionName(), while referencing uses::functionName.Final Answer:
A reference to the function named functionName -> Option DQuick Check:
Function reference = ::functionName [OK]
- Confusing function reference (::) with function call (())
- Thinking :: declares a variable
- Assuming :: is a comment or annotation
map in Kotlin?Solution
Step 1: Identify how to pass function references in Kotlin
Function references use the::operator before the function name when passed as arguments.Step 2: Check each option for correct syntax
list.map(::functionName) uses::functionName, which is the correct way to pass a function reference tomap.Final Answer:
list.map(::functionName) -> Option AQuick Check:
Pass function reference with :: [OK]
- Passing function call instead of reference (using parentheses)
- Omitting :: operator
- Using incorrect single colon : instead of double ::
fun square(x: Int) = x * x val numbers = listOf(1, 2, 3) val squares = numbers.map(::square) println(squares)
Solution
Step 1: Understand the function reference usage
The::squarepasses thesquarefunction tomap, which applies it to each element.Step 2: Calculate the result of applying square to each number
Applyingsquareto 1, 2, 3 gives 1, 4, 9 respectively.Final Answer:
[1, 4, 9] -> Option AQuick Check:
map(::square) squares each element [OK]
- Expecting original list instead of transformed list
- Confusing function reference with function call
- Thinking output is function names instead of results
fun greet(name: String) = "Hello, $name"
val greeter = greet
println(greeter("Alice"))Solution
Step 1: Check how function references are assigned
Assigning a function to a variable requires the::operator to get a reference.Step 2: Identify the missing :: operator
The code assignsgreetinstead of::greet, causing a type mismatch error.Final Answer:
Cannot assign function directly without :: operator -> Option BQuick Check:
Use :: to assign function reference [OK]
- Assigning function name without ::
- Adding parentheses which calls function instead of referencing
- Assuming function must return Unit
fun isEven(n: Int) = n % 2 == 0 val numbers = listOf(1, 2, 3, 4, 5, 6) val evenNumbers = numbers.filter(::isEven) println(evenNumbers)
Choose the correct output and explain why function references are useful here.
Solution
Step 1: Understand the function reference with filter
The::isEvenpasses theisEvenfunction tofilter, which keeps elements where the function returns true.Step 2: Apply isEven to each number and filter
Numbers 2, 4, and 6 are even, so they remain in the filtered list.Step 3: Explain usefulness of function references
Function references let us pass existing functions easily without writing extra lambdas.Final Answer:
[2, 4, 6] -> Option CQuick Check:
filter(::isEven) keeps even numbers [OK]
- Confusing filter with map output
- Expecting boolean list instead of filtered list
- Not using :: for function reference
Ready to prove it?
Pass the test and this pattern moves unknown
