0
0
Kotlinprogramming~10 mins

Function references (::functionName) in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Function references (::functionName)
Define function
Use :: to get reference
Pass reference as argument
Call function via reference
Get result/output
This flow shows how a function is defined, referenced using ::, passed around, and called later.
Execution Sample
Kotlin
fun greet(name: String) = "Hello, $name!"

fun main() {
    val greeter = ::greet
    println(greeter("Alice"))
}
This code defines a function greet, gets its reference with ::greet, assigns it to greeter, and calls it.
Execution Table
StepActionEvaluationResult/Output
1Define function greetfun greet(name: String) = "Hello, $name!"Function greet ready
2Get reference ::greet::greetFunction reference assigned to greeter
3Call greeter("Alice")greeter("Alice") calls greet("Alice")"Hello, Alice!"
4Print outputprintln(...) prints resultHello, Alice!
💡 Program ends after printing the greeting.
Variable Tracker
VariableStartAfter Step 2After Step 3Final
greeterundefinedFunction reference to greetN/A (called)Function reference to greet
Key Moments - 3 Insights
Why do we use ::greet instead of just greet?
Using ::greet gets the function itself as a value (a reference), not calling it. See execution_table step 2 where ::greet assigns the reference.
What happens when we call greeter("Alice")?
Calling greeter("Alice") actually calls the original greet function with "Alice". See execution_table step 3 for this call and result.
Can we store function references in variables?
Yes, variables like greeter can hold function references, letting us call the function later. This is shown in variable_tracker and steps 2-3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is stored in greeter after step 2?
AA function reference to greet
BThe string "Hello, Alice!"
CThe result of calling greet
DNothing, greeter is undefined
💡 Hint
Check variable_tracker column 'After Step 2' for greeter's value.
At which step does the program print the greeting message?
AStep 1
BStep 4
CStep 3
DStep 2
💡 Hint
Look at execution_table row with action 'Print output'.
If we replaced ::greet with greet() in step 2, what would happen?
Agreeter would be null
Bgreeter would hold the function reference
Cgreeter would hold the result of greet called with no arguments (error)
DProgram would print "Hello, Alice!" immediately
💡 Hint
Calling greet() without arguments causes error; ::greet gets reference without calling.
Concept Snapshot
Function references (::functionName) let you treat functions as values.
Use :: before a function name to get its reference.
Store references in variables to call functions later.
Calling the reference runs the original function.
Useful for passing functions as arguments or callbacks.
Full Transcript
This example shows how Kotlin functions can be referenced using the :: operator. First, a function greet is defined that takes a name and returns a greeting string. Then, ::greet gets the function reference and assigns it to a variable greeter. Calling greeter("Alice") calls the original greet function with "Alice" and returns "Hello, Alice!". Finally, the result is printed. The execution table traces each step: defining the function, getting the reference, calling via the reference, and printing the output. The variable tracker shows greeter holds the function reference after step 2. Key moments clarify why :: is needed to get the reference, how calling the reference works, and that variables can store function references. The quiz tests understanding of what is stored, when output happens, and what happens if :: is omitted. This teaches how function references enable flexible, reusable code by treating functions as values.