1 00:00:05,240 --> 00:00:10,400 In this video, we'll look at the structure or the syntax of a lambda expression. 2 00:00:11,340 --> 00:00:17,370 The first time you see a lambda expression, the syntax might look intimidating, but lambda expressions 3 00:00:17,370 --> 00:00:20,430 actually have a pretty simple and regular structure. 4 00:00:20,700 --> 00:00:24,660 In this slide, we can see the basic structure of a lambda expression. 5 00:00:24,690 --> 00:00:27,330 Let's look at each of its elements one by one. 6 00:00:28,620 --> 00:00:31,570 First, we have a set of square brackets. 7 00:00:31,590 --> 00:00:37,740 This defines the beginning of the lambda expression and is also the capture list that allows us to capture 8 00:00:37,740 --> 00:00:42,450 the context or the closure in which the Lambda Expression executes. 9 00:00:42,480 --> 00:00:47,490 We'll see this in more detail when we talk about stateful lambda expressions in a couple of videos. 10 00:00:47,490 --> 00:00:52,500 Depending on what we put in these square brackets, we can tell the compiler what elements we want to 11 00:00:52,500 --> 00:00:55,410 capture and whether by value or reference. 12 00:00:55,710 --> 00:00:59,140 The next part of the lambda expression is the parameter list. 13 00:00:59,160 --> 00:01:05,129 Every time the lambda executes, whatever parameters we specify in this parameter list will be passed 14 00:01:05,129 --> 00:01:06,070 into the lambda. 15 00:01:06,090 --> 00:01:09,600 This works very much like the parameter list in a function call. 16 00:01:09,810 --> 00:01:13,110 Next we have an arrow operator followed by a type. 17 00:01:13,440 --> 00:01:16,950 This is the type that's returned by the lambda expression. 18 00:01:17,280 --> 00:01:20,400 If the return is void, then we can omit this. 19 00:01:20,520 --> 00:01:26,130 Also, if the lambda is pretty simple, the compiler can generally deduce the return type of the lambda 20 00:01:26,130 --> 00:01:27,510 and you can omit this. 21 00:01:27,540 --> 00:01:30,120 Think of this as the return type of the function. 22 00:01:31,270 --> 00:01:35,570 The next part of the lambda expression is optional specifiers. 23 00:01:35,590 --> 00:01:39,210 The specifiers are mutable and context poor. 24 00:01:39,580 --> 00:01:43,960 I'll show you mutable in the ID and context, but will leave for another time. 25 00:01:44,260 --> 00:01:48,340 And the last part of the lambda expression is a set of curly braces. 26 00:01:48,340 --> 00:01:51,040 And this is where you write the code you want to execute. 27 00:01:51,400 --> 00:01:52,060 That's it. 28 00:01:52,060 --> 00:01:53,580 As you can see, it's pretty regular. 29 00:01:53,590 --> 00:01:56,140 So now let's look at a few lambda expressions. 30 00:01:58,880 --> 00:02:02,210 This is about a simple, a lambda expression as we can have. 31 00:02:02,480 --> 00:02:06,770 There's no capture list, there's no parameter list, there's no return type. 32 00:02:06,770 --> 00:02:08,960 And the body of the lambda simply displays. 33 00:02:08,960 --> 00:02:09,370 Hi. 34 00:02:09,380 --> 00:02:11,690 So let's see how we can use this lambda expression. 35 00:02:14,030 --> 00:02:15,990 Notice the function call operator. 36 00:02:16,010 --> 00:02:19,700 Those are the two parentheses in red on the right side of the statement. 37 00:02:20,120 --> 00:02:26,120 This will instantiate a function object from this lambda expression and call the function object using 38 00:02:26,120 --> 00:02:27,890 the overloaded function call operator. 39 00:02:27,890 --> 00:02:29,480 So this will display high. 40 00:02:29,900 --> 00:02:34,880 You don't typically see lambda expressions use this way, but you can clearly see how they work in this 41 00:02:34,880 --> 00:02:35,660 example. 42 00:02:37,330 --> 00:02:38,770 Here's another example. 43 00:02:38,830 --> 00:02:43,570 In this example, the lambda expression has a single integer X in the parameter list. 44 00:02:43,570 --> 00:02:48,700 When the lambda is instantiated and called, an integer will be passed into it and it will be available 45 00:02:48,700 --> 00:02:53,770 to the code in the lambda expression, just like a parameter would be available to the code and a regular 46 00:02:53,770 --> 00:02:54,490 function. 47 00:02:56,520 --> 00:03:01,980 In this example, we have two integers in the parameter list and the body simply displays the sum of 48 00:03:01,980 --> 00:03:03,060 the two integers. 49 00:03:04,230 --> 00:03:06,990 So let's go a little bit further with the lambda expression. 50 00:03:07,290 --> 00:03:10,200 We can assign lambda expressions to variables. 51 00:03:10,350 --> 00:03:15,150 We can use auto to tell the compiler to deduce the type of the land expression. 52 00:03:15,570 --> 00:03:21,030 The type is actually a function with template arguments, and I'll show you that in the ID a bit later. 53 00:03:21,030 --> 00:03:26,160 Now that we have a variable, we can call the function object created from the land expression. 54 00:03:26,160 --> 00:03:31,680 And as you can see in this case we're calling L and it displays hi and it looks just like a function 55 00:03:31,680 --> 00:03:32,190 call. 56 00:03:34,120 --> 00:03:38,590 This is a similar example, except that we have a single integer X in the parameter list of the lambda 57 00:03:38,590 --> 00:03:39,430 expression. 58 00:03:39,640 --> 00:03:43,360 So we call L and we pass in a ten and ten is displayed. 59 00:03:43,360 --> 00:03:46,780 We can call L and pass in 101 hundred is displayed. 60 00:03:47,110 --> 00:03:52,270 Again, you can see that the syntax is much simpler than creating a function object class like we did 61 00:03:52,270 --> 00:03:52,930 before. 62 00:03:54,620 --> 00:04:00,140 Now, if we want to provide a return value from the lambda expression, we could do it explicitly with 63 00:04:00,140 --> 00:04:03,070 the arrow and the return type as in the first example. 64 00:04:03,080 --> 00:04:09,620 But this is optional since the compiler can very often deduce the type from the return statement itself. 65 00:04:09,920 --> 00:04:15,980 So it's much more common to see these types of expressions written as in the second example. 66 00:04:16,640 --> 00:04:22,400 Since this lambda expression expects to integers and returns there some, we can call l the same way 67 00:04:22,400 --> 00:04:23,690 we can call a function. 68 00:04:24,050 --> 00:04:29,830 All of these lambda expressions that I've shown you so far are examples of stateless lambda expressions. 69 00:04:29,840 --> 00:04:33,350 These are lambda expressions that have empty capture lists. 70 00:04:33,470 --> 00:04:38,750 In the next video, we'll head to the ID and I'll create some more stateless lambda expressions and 71 00:04:38,750 --> 00:04:40,430 we'll use them in a few different ways.