laern-dart-the-hard-way

Learn Dart the Hard Way

Lambda, Higher-Order functions, and Lexical Closures have some similarities. In their namelessness and anonymity, these features of Dart are very interesting. Let us start with Lambda. Then we will discuss Higher-Order Functions and Closures. In reality, you will find that Lambda actually implements Higher-Order Functions.
Today I am going to talk about the Lambda only.
Next day, I will write about the other anonymous functions.

# Lambda: The Anonymous Function

As the name suggests, Lambda is a nameless function and we can use it in two ways. We can use it in a traditional method and also we can use the ‘Fat Arrow’.
Consider the first code snippet:

```//code 8.1 class LambdaCode{ // here addingTwonumbers is a nameless function Function addingTwonumbers = (int x, int y){ var sum = x + y; return sum; }; } main(List arguments){ var lambdaShow = LambdaCode(); print(lambdaShow.addingTwonumbers(12, 47)); }```

I will give the output after adding the ‘Fat Arrow’ method. The whole code snippet looks like this:

```//code 8.2 class LambdaCode{ // here addingTwonumbers is a nameless function Function addingTwonumbers = (int x, int y){ var sum = x + y; return sum; }; Function divideByFour = (int num) => num ~/ 4; } main(List arguments){ var lambdaShow = LambdaCode(); print(lambdaShow.addingTwonumbers(12, 47)); print(lambdaShow.divideByFour(56)); }```

The output is quite expected, in the first anonymous function ‘Function addingTwonumbers’ we have passed two parameters and added them. And using the ‘Fat Arrow’ method, we have passed a number through another nameless function ‘Function divideByFour’ and divided it by 4.

```//output of code 8.2 59 14```

While building a native iOS or Android app, you will see how these nameless functions come to your help.