What is Anonymous function in Dart Programming? How to implement it?

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 –

  1. bool topVariable = true;  
  2.   
  3. void main() {  
  4.   var inside_Main = true;  
  5.  // Defining Nested Function   
  6.    
  7.  void myFunction() {  
  8.     var inside_Function = true;  
  9.      
  10.  void nestedFunction() {  
  11.       var inside_NestedFunction = true;  
  12.       // This function is using all variable of the previous functions.  
  13.       assert(topVariable);  
  14.       assert(inside_Main);  
  15.       assert(inside_Function);  
  16.       assert(inside_NestedFunction);  
  17.     }  
  18.   }  
  19. }  

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 –

  1. void main() {  
  2.  String initial() {  
  3.      var name = ‘Will Smith’; // name is a local variable created by init  
  4.     
  5.      void disp_Name() { // displayName() is the inner function, a closure  
  6.            print(name); // use variable declared in the parent function  
  7.   }  
  8.   disp_Name();  
  9. }  
  10. 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.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More