‫Welcome back.
‫In this section we're going to learn about lambda expressions.
‫Lambda expressions in Kotlin are a concise way to define anonymous functions.
‫They are often used for passing functions as arguments, defining small inline functions and making
‫code more readable and expressive.
‫Lambda expressions are a fundamental feature of functional programming in Kotlin.
‫I'll start with the first type of lambda expressions, which is the basic lambda expression.
‫A basic lambda expression in Kotlin is a concise way to define anonymous functions with a simple structure.
‫The basic syntax of a lambda expression is.
‫Parameters body separated with this arrow and enclosed with these curly braces.
‫The curly braces denote the start of the lambda expression parameters.
‫These are the input parameters that a lambda expression can accept.
‫The parameters are specified in the form of parameter lists separated by commas.
‫You can have zero or more parameters.
‫The arrow operator separates the parameter list from the body of the lambda expression and the body.
‫This is the actual code or expression that the lambda expression will execute.
‫It's enclosed in the lambdas.
‫Curly braces.
‫Lambda expressions can be assigned to variables or passed as arguments to functions.
‫They are commonly used with higher order functions like map, filter for each and etc., where a function
‫is expected as an argument.
‫Let's start with a simple example of a basic lambda expression that adds two numbers val add integer.
‫Integer is returning type is int equals to a, b gives us a plus b in this example, we defined a lambda
‫expression that takes two integer parameters a and b, and return the sum of a and b as an integer.
‫These are the parameters, and this is the body of the lambda expression separated by the arrow.
‫The body of the lambda expression calculates the sum of a and b.
‫The lambda expression is assigned and can be assigned to a variable.
‫So for example, val result equals to add.
‫And here I can pass five and three and the add which is a variable that this lambda expression is assigned
‫to.
‫It indicates that the two parameters are of type integer and the body and the returning type of the
‫body.
‫Calculations will be an integer.
‫Here in the result, I'm calling the add lambda with arguments five and three to compute and print the
‫result.
‫So if I print ln result, I can get eight as a result.
‫And here we go.
‫So this is instead of creating a new function taking it to the parameter a and b of type int and the
‫returning type should be a plus b.
‫This is a lambda expression.
‫It's very simple that can do the job.
‫Basic lambda expressions are a fundamental building block in Kotlin for functional programming, and
‫are used to make your code more concise and expressive, especially when working with collections and
‫higher order functions.