‫Welcome back.
‫A lambda expression in Kotlin is a way to define a small, anonymous function that can be treated as
‫a variable.
‫It's a concise way to declare and pass behavior in your code.
‫Lambda expressions are also known as anonymous functions or simply lambdas.
‫In Kotlin, lambdas are often used with higher order functions to provide custom behavior to those functions.
‫This is an ordinary function and we learned how to create functions in the previous videos.
‫Add two numbers it took into the parameter x and y of type integer and it's returning type is an int.
‫And here is the method body returning x plus y, making the addition of x and y as a lambda, which
‫is a function without a name.
‫We can simply write this x comma y.
‫The arrow operator x plus y.
‫This is similar and equivalent to the function that we created before.
‫There are two ways to execute the lambda.
‫Add parentheses after the variable name or call the invoke method.
‫I'm creating a lambda expression here called print welcome, and inside this variable I stored the println
‫and this is the beginning of this lambda expression.
‫I can use the parentheses after the variable name or call invoke method to execute a lambda expression.
‫Let's deep dive into the syntax of a lambda expression.
‫This is the general syntax of a lambda expression.
‫We'll start with the variable name, then the function returning type or the function type.
‫Data type one, data type two and the return type.
‫And here is lambda.
‫And these data types should match the data type you specify in the function type.
‫For example val add.
‫This is the variable name.
‫This is the function type and this is the lambda.
‫This lambda expression will return the addition of a and b, which are the parameters passed and of
‫type integer.
‫There is a shorter syntax of this lambda expression.
‫We can write the same lambda expression in a shorter way, and we can omit the function type.
‫So we removed this function type declaration.
‫Also, we can create another shorter syntax.
‫But here we specify the function type.
‫But we don't reassign or re mention the data type of the parameters and the returning type of this lambda
‫expression.
‫Here I omitted the int int and the int data types.
‫There are a lot of function types and lambda types.
‫We're going to learn about them in the next videos.