Using Function References (::functionName) in Kotlin
📖 Scenario: You are building a simple Kotlin program that processes a list of numbers. You want to reuse existing functions by passing them as references instead of writing new code.
🎯 Goal: Learn how to use function references (::functionName) to pass functions as arguments and call them in Kotlin.
📋 What You'll Learn
Create a list of integers
Create a function that doubles a number
Use a function reference (::functionName) to apply the doubling function to each number in the list
Print the resulting list
💡 Why This Matters
🌍 Real World
Function references let you reuse existing functions easily when working with collections or callbacks, making your code cleaner and more readable.
💼 Career
Understanding function references is important for Kotlin developers to write concise and maintainable code, especially in Android app development and backend services.
Progress0 / 4 steps
1
Create a list of integers
Create a variable called numbers and assign it a list containing these integers: 1, 2, 3, 4, 5.
Kotlin
Hint
Use val numbers = listOf(1, 2, 3, 4, 5) to create the list.
2
Create a function that doubles a number
Create a function called double that takes an Int parameter named n and returns n * 2.
Kotlin
Hint
Define the function with fun double(n: Int): Int { return n * 2 }.
3
Use a function reference to apply doubling
Create a variable called doubledNumbers and assign it the result of calling numbers.map(::double) to apply the double function to each number.
Kotlin
Hint
Use val doubledNumbers = numbers.map(::double) to apply the function reference.
4
Print the resulting list
Write a println statement to display the doubledNumbers list.
Kotlin
Hint
Use println(doubledNumbers) to show the doubled list.
Practice
(1/5)
1. What does the Kotlin syntax ::functionName represent?
easy
A. A call to the function named functionName
B. A comment about the function named functionName
C. A variable declaration named functionName
D. A reference to the function named functionName
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 like functionName(), while referencing uses ::functionName.
Final Answer:
A reference to the function named functionName -> Option D
Quick Check:
Function reference = ::functionName [OK]
Hint: Remember :: means 'point to function', not call it [OK]
Common Mistakes:
Confusing function reference (::) with function call (())
Thinking :: declares a variable
Assuming :: is a comment or annotation
2. Which of the following is the correct syntax to pass a function reference to map in Kotlin?
easy
A. list.map(::functionName)
B. list.map(:functionName)
C. list.map(functionName())
D. list.map(functionName)
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 to map.
Final Answer:
list.map(::functionName) -> Option A
Quick Check:
Pass function reference with :: [OK]
Hint: Use :: before function name when passing as argument [OK]
Common Mistakes:
Passing function call instead of reference (using parentheses)
Omitting :: operator
Using incorrect single colon : instead of double ::
3. What is the output of this Kotlin code?
fun square(x: Int) = x * x
val numbers = listOf(1, 2, 3)
val squares = numbers.map(::square)
println(squares)
medium
A. [1, 4, 9]
B. [square, square, square]
C. [1, 2, 3]
D. Compilation error
Solution
Step 1: Understand the function reference usage
The ::square passes the square function to map, which applies it to each element.
Step 2: Calculate the result of applying square to each number