0
0
Kotlinprogramming~10 mins

Local functions (nested functions) in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Local functions (nested functions)
Start outer function
Define local function
Call local function
Local function executes
Return from outer function
The outer function starts, defines a local function inside it, calls this local function, then finishes.
Execution Sample
Kotlin
fun greet(name: String) {
    fun sayHello() {
        println("Hello, $name!")
    }
    sayHello()
}
Defines a function greet with a nested local function sayHello that prints a greeting using the outer function's parameter.
Execution Table
StepActionEvaluationOutput
1Call greet("Alice")Outer function starts
2Define local function sayHellosayHello is ready to use
3Call sayHello()sayHello executesHello, Alice!
4sayHello returnsControl back to greet
5greet returnsFunction ends
💡 greet function finishes after calling local function sayHello
Variable Tracker
VariableStartAfter greet callAfter sayHello callFinal
nameundefined"Alice""Alice""Alice"
sayHelloundefinedfunction definedfunction executedfunction no longer used
Key Moments - 2 Insights
Why can sayHello access the variable 'name'?
Because sayHello is defined inside greet, it can use greet's parameters like 'name' as shown in execution_table step 3.
Can sayHello be called outside greet?
No, sayHello is local to greet and only exists during greet's execution, as seen in execution_table steps 2 and 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is printed at step 3?
A"Hello, World!"
B"Hello, Alice!"
CNo output
DError
💡 Hint
Check the Output column at step 3 in the execution_table.
At which step does the local function sayHello get defined?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the Action column in execution_table for when sayHello is defined.
If we try to call sayHello outside greet, what happens?
AIt causes an error
BIt works normally
CIt prints "Hello, Alice!" anyway
DIt calls greet automatically
💡 Hint
Refer to key_moments about scope and local function accessibility.
Concept Snapshot
Local functions are functions defined inside another function.
They can access outer function variables.
They only exist during the outer function's execution.
Use them to organize code inside a function.
Syntax: define function inside another with fun keyword.
Call local function like any other function inside outer function.
Full Transcript
This example shows a Kotlin function greet that has a local function sayHello inside it. When greet is called with a name, it defines sayHello, which prints a greeting using the name parameter. Then greet calls sayHello, which prints "Hello, Alice!". After that, sayHello returns control to greet, and greet finishes. The local function can use variables from the outer function because it is nested inside it. However, sayHello cannot be called outside greet because it only exists inside greet's scope. This helps keep code organized and limits function visibility.