Welcome back.
In 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 lambda expression?
A lambda for a lambda expression?
Both as one is a function which has no name.
But another expression is not the same as an anonymous function, which also has no name.
So that there is 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 to unclick event so that the expressions and
anonymous functions are functional literals, for example, functions that are not declared but passed
immediately as an expression.
So Alanda is defined with curly braces, which takes verbals as a barometer, if any, and the 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 lambda, 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 two arguments and be at number five chromatin are the two values that we pass.
So A is five and besten 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 in this case.
Just add, 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 try to set an example using expressions.
So here we have some intent that's going to return.
And so what we're saying is that we creating a lambda expression called sum and we take two parameters,
end an end or type end and we return it.
So what we are now saying that the expression itself should be A off type B of type and shall return
A plus B.
OK, so this is what we do here.
So what is after?
The lambda expression is what is going to be returned because we defined it here.
OK, but it's possible to do this even shorter.
Of course we had this principle and there in order to display something.
So we are calling this some lambda expression.
We passing the two values and we're printing it because we also want to print the value.
Right.
But we can do this even shorter so we can have some A of type B of type is already part of the lambda
expression.
And then we just printed an A plus B.
So now if we call some, we passed the two values that will already print 15.
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 we're 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.
OK, 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.
OK, so you still have to follow the structure of whatever you're trying to return.
It has to be returned.
OK, so whatever you're defining that will be returned to this case.
An integer has to be returned here as a return statement.
All right.
So this is an example of a lambda expression and we're going to use some 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 the concept behind it and not be surprised when we see
them rather than having to use them the expressions yourself all the time because they are a little
more advanced than the other ways of creating function.
So feel free to use either of them.