1 00:00:00,000 --> 00:00:02,011 (upbeat electronic music) 2 00:00:02,011 --> 00:00:05,250 (wind swooshes) (keyboard keys clack) 3 00:00:05,250 --> 00:00:06,660 Frank: In this video, we'll look 4 00:00:06,660 --> 00:00:10,353 at the structure or the syntax of a lambda expression. 5 00:00:11,370 --> 00:00:13,740 The first time you see a lambda expression, 6 00:00:13,740 --> 00:00:15,930 the syntax might look intimidating, 7 00:00:15,930 --> 00:00:19,050 but lambda expressions actually have a pretty simple 8 00:00:19,050 --> 00:00:20,790 and regular structure. 9 00:00:20,790 --> 00:00:21,623 In this slide, 10 00:00:21,623 --> 00:00:24,810 we can see the basic structure of a lambda expression. 11 00:00:24,810 --> 00:00:27,303 Let's look at each of its elements one by one. 12 00:00:28,650 --> 00:00:31,710 First we have a set of square brackets. 13 00:00:31,710 --> 00:00:34,860 This defines the beginning of the lambda expression, 14 00:00:34,860 --> 00:00:37,140 and is also the capture list that allows us 15 00:00:37,140 --> 00:00:40,170 to capture the context or the closure 16 00:00:40,170 --> 00:00:42,570 in which the lambda expression executes. 17 00:00:42,570 --> 00:00:44,490 We'll see this in more detail when we talk 18 00:00:44,490 --> 00:00:46,410 about stateful lambda expressions 19 00:00:46,410 --> 00:00:47,550 in a couple of videos. 20 00:00:47,550 --> 00:00:50,040 Depending on what we put in these square brackets, 21 00:00:50,040 --> 00:00:52,350 we can tell the compiler what elements we want 22 00:00:52,350 --> 00:00:55,800 to capture, and whether by value or reference. 23 00:00:55,800 --> 00:00:56,633 The next part 24 00:00:56,633 --> 00:00:59,250 of the lambda expression is the parameter list. 25 00:00:59,250 --> 00:01:01,140 Every time the lambda executes, 26 00:01:01,140 --> 00:01:03,060 whatever parameters we specify 27 00:01:03,060 --> 00:01:06,240 in this parameter list will be passed into the lambda. 28 00:01:06,240 --> 00:01:07,320 This works very much 29 00:01:07,320 --> 00:01:09,960 like the parameter list In a function call. 30 00:01:09,960 --> 00:01:13,590 Next, we have an arrow operator followed by a type. 31 00:01:13,590 --> 00:01:17,430 This is the type that's returned by the lambda expression. 32 00:01:17,430 --> 00:01:20,700 If the return is void, then we can omit this. 33 00:01:20,700 --> 00:01:22,770 Also, if the lambda is pretty simple, 34 00:01:22,770 --> 00:01:25,380 the compiler can generally deduce the return type 35 00:01:25,380 --> 00:01:27,540 of the lambda, and you can omit this. 36 00:01:27,540 --> 00:01:30,063 Think of this as the return type of the function. 37 00:01:31,290 --> 00:01:32,123 The next part 38 00:01:32,123 --> 00:01:35,670 of the lambda expression is optional specifiers. 39 00:01:35,670 --> 00:01:39,600 The specifiers are mutable and constexpr. 40 00:01:39,600 --> 00:01:41,520 I'll show you mutable in the IDE, 41 00:01:41,520 --> 00:01:44,430 and constexpr we'll leave for another time. 42 00:01:44,430 --> 00:01:45,330 And the last part 43 00:01:45,330 --> 00:01:48,390 of the lambda expression is a set of curly braces, 44 00:01:48,390 --> 00:01:51,510 and this is where you write the code you want to execute. 45 00:01:51,510 --> 00:01:53,640 That's it, as you can see, it's pretty regular. 46 00:01:53,640 --> 00:01:56,193 So now let's look at a few lambda expressions. 47 00:01:58,950 --> 00:02:02,580 This is about as simple a lambda expression as we can have. 48 00:02:02,580 --> 00:02:05,340 There's no capture list, there's no parameter list, 49 00:02:05,340 --> 00:02:07,500 there's no return type, and the body 50 00:02:07,500 --> 00:02:09,509 of the lambda simply displays Hi. 51 00:02:09,509 --> 00:02:12,060 So let's see how we can use this lambda expression. 52 00:02:14,040 --> 00:02:16,020 Notice the function call operator. 53 00:02:16,020 --> 00:02:16,910 Those are the two parentheses 54 00:02:16,910 --> 00:02:20,250 in red on the right side of the statement. 55 00:02:20,250 --> 00:02:22,500 This will instantiate a function object 56 00:02:22,500 --> 00:02:24,300 from this lambda expression 57 00:02:24,300 --> 00:02:25,770 and call the function object 58 00:02:25,770 --> 00:02:27,960 using the overloaded function call operator. 59 00:02:27,960 --> 00:02:30,030 So this will display Hi. 60 00:02:30,030 --> 00:02:32,970 You don't typically see lambda expressions used this way, 61 00:02:32,970 --> 00:02:35,670 but you can clearly see how they work in this example. 62 00:02:37,320 --> 00:02:38,970 Here's another example. 63 00:02:38,970 --> 00:02:39,803 In this example, 64 00:02:39,803 --> 00:02:42,140 the lambda expression has a single integer, x, 65 00:02:42,140 --> 00:02:43,710 in the parameter list. 66 00:02:43,710 --> 00:02:45,720 When the lambda is instantiated and called, 67 00:02:45,720 --> 00:02:47,730 an integer will be passed into it, 68 00:02:47,730 --> 00:02:50,850 and it'll be available to the code in the lambda expression, 69 00:02:50,850 --> 00:02:52,680 just like a parameter would be available 70 00:02:52,680 --> 00:02:54,453 to the code in a regular function. 71 00:02:56,550 --> 00:02:59,637 In this example, we have two integers in the parameter list, 72 00:02:59,637 --> 00:03:03,153 and the body simply displays the sum of the two integers. 73 00:03:04,350 --> 00:03:07,410 So let's go a little bit further with the lambda expression. 74 00:03:07,410 --> 00:03:10,500 We can assign lambda expressions to variables. 75 00:03:10,500 --> 00:03:13,710 We can use auto to tell the compiler to deduce the type 76 00:03:13,710 --> 00:03:15,690 of the lambda expression. 77 00:03:15,690 --> 00:03:17,820 The type is actually a std function 78 00:03:17,820 --> 00:03:18,900 with template arguments, 79 00:03:18,900 --> 00:03:21,090 and I'll show you that in the IDE a bit later. 80 00:03:21,090 --> 00:03:22,590 Now that we have a variable, 81 00:03:22,590 --> 00:03:24,405 we can call the function object created 82 00:03:24,405 --> 00:03:26,310 from the lambda expression. 83 00:03:26,310 --> 00:03:28,980 And as you can see, in this case, we're calling l, 84 00:03:28,980 --> 00:03:30,270 and it displays Hi. 85 00:03:30,270 --> 00:03:32,223 And it looks just like a function call. 86 00:03:34,170 --> 00:03:35,085 This is a similar example, 87 00:03:35,085 --> 00:03:37,320 except that we have a single integer, x, 88 00:03:37,320 --> 00:03:39,750 in the parameter list of the lambda expression. 89 00:03:39,750 --> 00:03:43,410 So we call l and we pass in a 10, and 10 is displayed. 90 00:03:43,410 --> 00:03:47,130 We can call l and pass in a 100 and a 100 is displayed. 91 00:03:47,130 --> 00:03:49,439 Again, you can see that the syntax is much simpler 92 00:03:49,439 --> 00:03:52,923 than creating a function object class, like we did before. 93 00:03:54,720 --> 00:03:57,030 Now if we want to provide a return value 94 00:03:57,030 --> 00:03:59,451 from the lambda expression, we could do it explicitly 95 00:03:59,451 --> 00:04:01,295 with the arrow and the return type, 96 00:04:01,295 --> 00:04:03,270 as in the first example. 97 00:04:03,270 --> 00:04:05,321 But this is optional, since the compiler can 98 00:04:05,321 --> 00:04:07,560 very often deduce the type 99 00:04:07,560 --> 00:04:10,050 from the return statement itself. 100 00:04:10,050 --> 00:04:12,690 So it's much more common to see these types 101 00:04:12,690 --> 00:04:16,769 of lambda expressions written as in the second example. 102 00:04:16,769 --> 00:04:19,320 Since this lambda expression expects two integers 103 00:04:19,320 --> 00:04:20,790 and returns their sum, 104 00:04:20,790 --> 00:04:24,120 we can call l the same way we can call a function. 105 00:04:24,120 --> 00:04:25,615 All of these lambda expressions 106 00:04:25,615 --> 00:04:27,810 that I've shown you so far are examples 107 00:04:27,810 --> 00:04:29,940 of stateless lambda expressions. 108 00:04:29,940 --> 00:04:33,570 These are lambda expressions that have empty capture lists. 109 00:04:33,570 --> 00:04:35,790 In the next video, we'll head to the IDE 110 00:04:35,790 --> 00:04:38,670 and I'll create some more stateless lambda expressions, 111 00:04:38,670 --> 00:04:40,820 and we'll use them in a few different ways.