Recall & Review
beginner
What is a local function in Kotlin?
A local function is a function defined inside another function. It helps organize code by grouping related tasks within the outer function.
Click to reveal answer
beginner
How do local functions help in Kotlin programming?
Local functions improve readability and structure by keeping helper code close to where it is used. They also limit the scope, so the local function can't be accessed outside its containing function.
Click to reveal answer
intermediate
Can a local function access variables from its outer function in Kotlin?
Yes, a local function can access and use variables declared in its outer function. This allows sharing data without passing parameters explicitly.
Click to reveal answer
beginner
Write a simple Kotlin function with a local function that prints a greeting.
fun greet() {
fun sayHello() {
println("Hello!")
}
sayHello()
}
Click to reveal answer
intermediate
Why might you choose a local function over a lambda in Kotlin?
Local functions can have names, making code clearer. They also support recursion and can be easier to debug compared to lambdas.
Click to reveal answer
Where can a local function be declared in Kotlin?
✗ Incorrect
Local functions are declared inside other functions, not at the top level or directly inside classes.
Can a local function access variables from its outer function in Kotlin?
✗ Incorrect
Local functions can access variables declared in their outer function's scope.
What is one advantage of using local functions in Kotlin?
✗ Incorrect
Local functions help keep code organized and limit their visibility to the containing function.
Which of these is a valid way to define a local function in Kotlin?
✗ Incorrect
Option A shows a local function 'inner' defined inside 'outer' and called inside it.
Why might you prefer a local function over a lambda expression in Kotlin?
✗ Incorrect
Local functions have names, making code clearer, and can call themselves recursively, unlike lambdas.
Explain what a local function is in Kotlin and how it can be useful.
Think about functions inside functions.
You got /4 concepts.
Describe how local functions can access variables from their outer function and why this is helpful.
Consider variable visibility inside nested functions.
You got /4 concepts.