In this video, we'll look at the structure or the syntax of a lambda expression.
The first time you see a lambda expression, the syntax might look intimidating, but lambda expressions
actually have a pretty simple and regular structure.
In this slide, we can see the basic structure of a lambda expression.
Let's look at each of its elements one by one.
First, we have a set of square brackets.
This defines the beginning of the lambda expression and is also the capture list that allows us to capture
the context or the closure in which the Lambda Expression executes.
We'll see this in more detail when we talk about stateful lambda expressions in a couple of videos.
Depending on what we put in these square brackets, we can tell the compiler what elements we want to
capture and whether by value or reference.
The next part of the lambda expression is the parameter list.
Every time the lambda executes, whatever parameters we specify in this parameter list will be passed
into the lambda.
This works very much like the parameter list in a function call.
Next we have an arrow operator followed by a type.
This is the type that's returned by the lambda expression.
If the return is void, then we can omit this.
Also, if the lambda is pretty simple, the compiler can generally deduce the return type of the lambda
and you can omit this.
Think of this as the return type of the function.
The next part of the lambda expression is optional specifiers.
The specifiers are mutable and context poor.
I'll show you mutable in the ID and context, but will leave for another time.
And the last part of the lambda expression is a set of curly braces.
And this is where you write the code you want to execute.
That's it.
As you can see, it's pretty regular.
So now let's look at a few lambda expressions.
This is about a simple, a lambda expression as we can have.
There's no capture list, there's no parameter list, there's no return type.
And the body of the lambda simply displays.
Hi.
So let's see how we can use this lambda expression.
Notice the function call operator.
Those are the two parentheses in red on the right side of the statement.
This will instantiate a function object from this lambda expression and call the function object using
the overloaded function call operator.
So this will display high.
You don't typically see lambda expressions use this way, but you can clearly see how they work in this
example.
Here's another example.
In this example, the lambda expression has a single integer X in the parameter list.
When the lambda is instantiated and called, an integer will be passed into it and it will be available
to the code in the lambda expression, just like a parameter would be available to the code and a regular
function.
In this example, we have two integers in the parameter list and the body simply displays the sum of
the two integers.
So let's go a little bit further with the lambda expression.
We can assign lambda expressions to variables.
We can use auto to tell the compiler to deduce the type of the land expression.
The type is actually a function with template arguments, and I'll show you that in the ID a bit later.
Now that we have a variable, we can call the function object created from the land expression.
And as you can see in this case we're calling L and it displays hi and it looks just like a function
call.
This is a similar example, except that we have a single integer X in the parameter list of the lambda
expression.
So we call L and we pass in a ten and ten is displayed.
We can call L and pass in 101 hundred is displayed.
Again, you can see that the syntax is much simpler than creating a function object class like we did
before.
Now, if we want to provide a return value from the lambda expression, we could do it explicitly with
the arrow and the return type as in the first example.
But this is optional since the compiler can very often deduce the type from the return statement itself.
So it's much more common to see these types of expressions written as in the second example.
Since this lambda expression expects to integers and returns there some, we can call l the same way
we can call a function.
All of these lambda expressions that I've shown you so far are examples of stateless lambda expressions.
These are lambda expressions that have empty capture lists.
In the next video, we'll head to the ID and I'll create some more stateless lambda expressions and
we'll use them in a few different ways.