Complete the code to define a local function inside main.
fun main() {
fun [1]() {
println("Hello from local function")
}
greet()
}The local function is called greet and is called inside main.
Complete the code to call the local function inside main.
fun main() {
fun greet() {
println("Hi there!")
}
[1]()
}The local function greet is called inside main to print the message.
Fix the error by completing the code to return the sum using a local function.
fun sumNumbers(a: Int, b: Int): Int {
fun add() = a [1] b
return add()
}The local function add returns the sum of a and b using the + operator.
Fill both blanks to create a local function that checks if a number is even.
fun main() {
fun [1](num: Int): Boolean {
return num [2] 2 == 0
}
println(isEven(4))
}/ instead of modulus %.The local function isEven checks if the number divided by 2 leaves a remainder of 0 using the modulus operator %.
Fill all three blanks to create a local function that filters and returns words longer than 3 letters.
fun main() {
val words = listOf("cat", "house", "dog", "elephant")
fun filterLongWords(list: List<String>): List<String> {
return list.filter { word -> word.[1] > [2] }
}
val result = filterLongWords(words)
println(result) // Should print words longer than [3] letters
}word.size which is not valid for strings.The local function uses word.length to get the length of each word and filters those longer than 3 letters.