kotlin arrow function
// Step 1: Define a function using arrow syntax
val add: (Int, Int) -> Int = { x, y -> x + y }
// Step 2: Call the function
val result = add(5, 3)
// Step 3: Print the result
println("Result: $result")
Explanation:
Declare a function named
add
with the arrow syntax. It takes two parameters of typeInt
and returns anInt
. The function body,{ x, y -> x + y }
, adds the two parameters and returns the result.Call the
add
function with arguments5
and3
. The result is assigned to the variableresult
.Print the result using
println
. The output will be "Result: 8" in this example.