Complete the code to declare a function variable that adds two numbers.
val add: (Int, Int) -> Int = [1]In Kotlin, you can assign a lambda expression to a variable to create a function value. The syntax { a, b -> a + b } defines a function that takes two integers and returns their sum.
Complete the code to pass a function as a parameter.
fun operate(x: Int, y: Int, operation: (Int, Int) -> Int): Int {
return [1](x, y)
}The parameter operation is a function that takes two integers and returns an integer. To use it, call operation(x, y).
Fix the error in the code to assign a function reference to a variable.
fun greet(name: String) = "Hello, $name" val greeter: (String) -> String = [1]
greet.name or greet{}.To assign a function reference in Kotlin, use ::greet without parentheses. Using ::greet assigns the function itself, not the result of calling it.
Fill both blanks to create a list of functions and call the first one.
val functions = listOf<(Int) -> Int>({ x -> x * 2 }, [1])
val result = functions[0]([2])
println(result)The list contains two functions. The second function is a lambda that adds 3. To call the first function with an argument, pass a number like 5.
Fill all three blanks to filter and map a list using functions.
val numbers = listOf(1, 2, 3, 4, 5) val filtered = numbers.filter [1] val mapped = filtered.map [2] println(mapped) // Output: [3]
The filter keeps numbers greater than 2. The map multiplies each by 10. So the output is [30, 40, 50].