Lambda function is also known as anonymous function because it has no name. Parameters are in the left side of the arrow and actual code is on the right side of the arrow. Don’t worry we will take an example to explain this.
How a Lambda function looks?
{my_var -> actual_code_implementation}
Kotlin Lambda function example
In the following example we have defined a lambda function to add two integer numbers. The lambda function is defined in curly braces, the left side of the arrow is our parameters with their data type and in the right side of the arrow is the body of function.
fun main(args: Array<String>){ //lambda function val sum = {num1: Int, num2: Int -> num1 + num2} println("10+5: ${sum(10,5)}") }
Output:
Leave a Reply