A function without a name is known as an anonymous function. They behave in the exact same manner as a normal named function would. The only difference between the named and an anonymous function is how different they are in syntax.
This type of function is known as an anonymous function, lambda, or closure. An anonymous function behaves the same as a regular function, but it does not have a name with it. It can have zero or any number of arguments with an optional type annotation.
Anonymous functions are used in Dart to form closures. An anonymous function contains a self-contained block of codes, also it can be passed as a parameter to another function as well.
Anonymous function Syntax
(parameterList){ // inner statement(s) }
Example
Now, let’s consider a simple example of an anonymous function.
Consider the example shown below −
void main() { var fruits = ["Apple", "Mango", "Banana", "Kiwi"]; fruits.forEach((item) { print('${fruits.indexOf(item)}: $item'); }); }
In the above example, we have an anonymous function with an untyped parameter named item.
Output
0: Apple 1: Mango 2: Banana 3: Kiwi
Lexical Scope
As we have discussed in the Dart introduction, it is a lexical scope language which means the variable’s scope is decided at compile-time. The scope of the variable is determined when code is compiled. The variable behaves differently if they defined in the different curly braces. Let’s understand the following example.
Example –
- bool topVariable = true;
- void main() {
- var inside_Main = true;
- // Defining Nested Function
- void myFunction() {
- var inside_Function = true;
- void nestedFunction() {
- var inside_NestedFunction = true;
- // This function is using all variable of the previous functions.
- assert(topVariable);
- assert(inside_Main);
- assert(inside_Function);
- assert(inside_NestedFunction);
- }
- }
- }
Observe the above code, the nestedFunction() used the variables of the previous function.
Lexical Closure
A lexical closure is referred to as a closure, is a function object that has access to variables in its lexical scope even when the function is used of its original scope. In other words, it provides access to an outer function’s scope from inner function. Let’s understand the following example.
Example –
- void main() {
- String initial() {
- var name = ‘Will Smith’; // name is a local variable created by init
- void disp_Name() { // displayName() is the inner function, a closure
- print(name); // use variable declared in the parent function
- }
- disp_Name();
- }
- init();
Output
Will Smith
Explanation:
In the above code, the initial() function created a local variable called name and function called disp_Name(). The disp_Name() function defined inside the initial() function and hence disp_Name() function has no local variable its own.
The inner function can access the variable of the outer functions. The function disp_Name() can access the name variable which is declared in the outer function, initial().
How to use Lambda functions in Dart programming
Lambda functions are a short and simple way to express tiny functions. Lambda functions are also known as anonymous functions or closures.They are functions without a name that can be used inline within the code.
return_type var_name = (parameters)=> expression;
Note: When using the Lambda function syntax, only one expression can be returned, and it must be a single-line expression.
Examples
#### How to use a Lambda function to add two numbers
// Using lambda function
final addNum = (int a, int b) => a + b;
main() {
print(addNum(15, 7));
}
//Output
22
Finally, if a function only returns one expression, we may use the Lambda function to represent it in only one line.