1 00:00:00,150 --> 00:00:00,870 Welcome back. 2 00:00:01,150 --> 00:00:06,450 And this video we're going to look at lambda expressions and lambda expressions are a pretty cool feature. 3 00:00:06,510 --> 00:00:13,560 They allow us to write super concise and short code that does quite some advanced things. 4 00:00:13,770 --> 00:00:14,670 So let's look at that. 5 00:00:15,180 --> 00:00:16,920 What is a lumba expression? 6 00:00:17,190 --> 00:00:23,640 A lambda or a lambda expression both as fine as a function which has no name, but I love the expression 7 00:00:23,640 --> 00:00:29,070 is not the same as an anonymous function, which also has no name so that there's a little distinction 8 00:00:29,070 --> 00:00:29,340 there. 9 00:00:29,580 --> 00:00:33,570 We're going to focus on lambda expressions for now, though, because these are the ones that we're 10 00:00:33,570 --> 00:00:37,230 going to use and that we already have used when we used the click event. 11 00:00:37,980 --> 00:00:43,530 So lambda expressions and anonymous functions are functional literals, for example, functions that 12 00:00:43,530 --> 00:00:47,070 are not declared but passed immediately as an expression. 13 00:00:47,970 --> 00:00:52,710 So lambda is defined with curly braces, which takes variables as a parameter, if any. 14 00:00:53,070 --> 00:00:58,500 And a body of a function so it can take variables as a parameter, but it doesn't have to. 15 00:00:58,890 --> 00:01:01,140 OK, so it's not a requirement. 16 00:01:01,740 --> 00:01:08,070 The body of a function is written after the variable, if any, followed by the Arrow operator. 17 00:01:08,190 --> 00:01:10,640 So this is the lambda operator, so to speak. 18 00:01:10,650 --> 00:01:13,590 So this is a dash with an arrow. 19 00:01:15,140 --> 00:01:23,030 And the syntax is variable or variables, lambda expression, body of lender, and all of that is surrounded 20 00:01:23,030 --> 00:01:23,840 by curly brackets. 21 00:01:24,560 --> 00:01:30,740 So let's look at a normal function addition of two numbers where we create a function, add numbers 22 00:01:31,070 --> 00:01:37,730 or add number passing to arguments and b add number five or 10 are the two values that we pass. 23 00:01:37,740 --> 00:01:43,130 So A is five and 10, and the function looks like this function. 24 00:01:43,130 --> 00:01:51,410 Add number A and B, and then we have the value add is a plus B, and then we print this case, just 25 00:01:51,410 --> 00:01:51,740 add. 26 00:01:52,250 --> 00:01:52,700 And that's it. 27 00:01:52,940 --> 00:01:55,310 So this is what our function does very basic. 28 00:01:56,030 --> 00:02:01,010 Now the thing is we can have this output of 15, but we can really shorten this. 29 00:02:01,700 --> 00:02:04,040 Now let's see how we can do that with a lambda expression. 30 00:02:04,400 --> 00:02:07,370 So we will write the same example using lambda expressions. 31 00:02:07,790 --> 00:02:11,570 So here we have some intent that's going to return. 32 00:02:11,840 --> 00:02:18,710 So what we're saying is that we are creating a lambda expression called sum, and we take two parameters 33 00:02:18,830 --> 00:02:22,670 and an int or of type int and we return and enter. 34 00:02:22,970 --> 00:02:27,080 So what we are now saying that the lambda expression itself should be a. 35 00:02:27,650 --> 00:02:31,100 Of type int b of type and shall return a plus b. 36 00:02:31,670 --> 00:02:34,100 OK, so this is what we do here. 37 00:02:34,280 --> 00:02:39,530 So what this after the lambda expression is what is going to be return because we defined it here? 38 00:02:40,100 --> 00:02:43,790 OK, but it's possible to do this even shorter. 39 00:02:44,330 --> 00:02:47,510 Of course, we had this print and there in order to display something. 40 00:02:47,510 --> 00:02:50,330 So we are calling this some lambda expression. 41 00:02:50,330 --> 00:02:54,120 We passing the two values and then we're printing it because we also wanted to print the value, right? 42 00:02:54,140 --> 00:03:02,030 But we can do this even shorter so we can have while some a of Typekit b of type int is already part 43 00:03:02,030 --> 00:03:05,330 of the lambda expression and then we just print L and a plus B. 44 00:03:05,840 --> 00:03:12,170 So now if we call some, we pass the two values that will already print fifteen, so it will directly 45 00:03:12,170 --> 00:03:12,980 print the result. 46 00:03:13,280 --> 00:03:14,750 So we just had two lines. 47 00:03:14,960 --> 00:03:19,760 It's not exactly the same as you can see in one of the two examples where actually returning. 48 00:03:19,770 --> 00:03:22,280 So in this case, we're returning the value first. 49 00:03:22,640 --> 00:03:27,020 And then we use it in a print line statements in order to print the return statement. 50 00:03:27,380 --> 00:03:34,550 And then in this case, we directly added to the print line here in order to display whatever is here. 51 00:03:34,700 --> 00:03:39,980 So if you wanted to return it, then you would get rid of this print line and then to the same print 52 00:03:39,980 --> 00:03:41,840 line, some as you had here at the top. 53 00:03:42,410 --> 00:03:47,300 So both versions are fine, but what you cannot do is use print line in here because you're saying that 54 00:03:47,300 --> 00:03:48,950 you return an integer. 55 00:03:49,370 --> 00:03:54,140 Okay, so you still have to follow the structure of whatever you're trying to return. 56 00:03:54,140 --> 00:03:55,400 It has to be returned. 57 00:03:55,770 --> 00:04:01,460 Okay, so whatever you defining that will be returned in this case, an integer has to be returned here 58 00:04:01,700 --> 00:04:02,720 as the return statement. 59 00:04:03,650 --> 00:04:04,040 All right. 60 00:04:04,220 --> 00:04:09,050 So this is an example of a lambda expression and we're going to use them the expressions. 61 00:04:09,320 --> 00:04:15,830 But more importantly, we're going to see lambda expressions in action when we work, for example, 62 00:04:15,830 --> 00:04:19,310 with events such as click events and in other cases as well. 63 00:04:19,550 --> 00:04:24,980 So it's more important for you to understand that concept behind it and not be surprised when we see 64 00:04:24,980 --> 00:04:30,410 them, rather than having to use them the expression to yourself all the time because they are a little 65 00:04:30,410 --> 00:04:33,500 more advanced than the other ways of creating functions. 66 00:04:33,500 --> 00:04:36,260 So feel free to use either of them.