(upbeat electronic music)
(wind swooshes) (keyboard keys clack)
Frank: 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 constexpr.
I'll show you mutable in the IDE,
and constexpr we'll 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 as 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 Hi.
You don't typically see lambda expressions used 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'll be available to the code in the lambda expression,
just like a parameter would be available
to the code in 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 lambda expression.
The type is actually a std function
with template arguments,
and I'll show you that in the IDE a bit later.
Now that we have a variable,
we can call the function object created
from the lambda 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 10, and 10 is displayed.
We can call l and pass in a 100 and a 100 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 lambda expressions written as in the second example.
Since this lambda expression expects two integers
and returns their sum,
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 IDE
and I'll create some more stateless lambda expressions,
and we'll use them in a few different ways.