/** * Functions hooked into colormag_action_head action. * * @hooked colormag_head - 10 */ do_action( 'colormag_action_head' ); wp_head(); ?>

Kotlin Lambda Function or Anonymous function

In this tutorial,we will learn about the kotlin lambda function with example

Lambda function is an anonymous function, i.e. function without name.It is surrounded by the curly brace { } in which parameter list or variables are listed to the left side and function of body are listed to the right side of the -> operator.

Syntax of Lambda function

{ list of parameter->function body }

How to declare the lambda function in a variable

val var_name:data_type={parameter_list->function_body}

Example

val sum={x:Int,y:Int-> a+b}

Now,Let’s try to understand the lambda function with the help of Example

fun main(args:Array<String>)
{
val add={a:Int,b:Int ->a+b} 

val add_result=add(10,20)

println(“sum of a+b is:$add_result")
}

Output:

Sum of a+b is 30

In the above example,the Lambda function takes the values from a & b parameters and gives the result in the form of a+b ie 30.

Advantage of Lambda function

It reduce the number of lines of code

Sequential and parallel  execution support by passing behaviour in methods

Higher efficiency

Thanks and Regards

Placement Preparation

sumankumar80

Hello Friends,I have written and developed this website because you can learn technology easily ,prepare for the placement and also for the any type of examination . By :Suman Kumar

Leave a Reply

Your email address will not be published. Required fields are marked *