Welcome back.
And this video we're going to look at lambda expressions and lambda expressions are a pretty cool feature.
They allow us to write super concise and short code that does quite some advanced things.
So let's look at that.
What is a lumba expression?
A lambda or a lambda expression both as fine as a function which has no name, but I love the expression
is not the same as an anonymous function, which also has no name so that there's a little distinction
there.
We're going to focus on lambda expressions for now, though, because these are the ones that we're
going to use and that we already have used when we used the click event.
So lambda expressions and anonymous functions are functional literals, for example, functions that
are not declared but passed immediately as an expression.
So lambda is defined with curly braces, which takes variables as a parameter, if any.
And a body of a function so it can take variables as a parameter, but it doesn't have to.
OK, so it's not a requirement.
The body of a function is written after the variable, if any, followed by the Arrow operator.
So this is the lambda operator, so to speak.
So this is a dash with an arrow.
And the syntax is variable or variables, lambda expression, body of lender, and all of that is surrounded
by curly brackets.
So let's look at a normal function addition of two numbers where we create a function, add numbers
or add number passing to arguments and b add number five or 10 are the two values that we pass.
So A is five and 10, and the function looks like this function.
Add number A and B, and then we have the value add is a plus B, and then we print this case, just
add.
And that's it.
So this is what our function does very basic.
Now the thing is we can have this output of 15, but we can really shorten this.
Now let's see how we can do that with a lambda expression.
So we will write the same example using lambda expressions.
So here we have some intent that's going to return.
So what we're saying is that we are creating a lambda expression called sum, and we take two parameters
and an int or of type int and we return and enter.
So what we are now saying that the lambda expression itself should be a.
Of type int b of type and shall return a plus b.
OK, so this is what we do here.
So what this after the lambda expression is what is going to be return because we defined it here?
OK, but it's possible to do this even shorter.
Of course, we had this print and there in order to display something.
So we are calling this some lambda expression.
We passing the two values and then we're printing it because we also wanted to print the value, right?
But we can do this even shorter so we can have while some a of Typekit b of type int is already part
of the lambda expression and then we just print L and a plus B.
So now if we call some, we pass the two values that will already print fifteen, so it will directly
print the result.
So we just had two lines.
It's not exactly the same as you can see in one of the two examples where actually returning.
So in this case, we're returning the value first.
And then we use it in a print line statements in order to print the return statement.
And then in this case, we directly added to the print line here in order to display whatever is here.
So if you wanted to return it, then you would get rid of this print line and then to the same print
line, some as you had here at the top.
So both versions are fine, but what you cannot do is use print line in here because you're saying that
you return an integer.
Okay, so you still have to follow the structure of whatever you're trying to return.
It has to be returned.
Okay, so whatever you defining that will be returned in this case, an integer has to be returned here
as the return statement.
All right.
So this is an example of a lambda expression and we're going to use them the expressions.
But more importantly, we're going to see lambda expressions in action when we work, for example,
with events such as click events and in other cases as well.
So it's more important for you to understand that concept behind it and not be surprised when we see
them, rather than having to use them the expression to yourself all the time because they are a little
more advanced than the other ways of creating functions.
So feel free to use either of them.