What is an anonymous function in Dart (Class 19)

What is an anonymous function in Dart

What is an anonymous function in Dart?

We have so far seen named or regular functions but now we will find the answer of, what is an anonymous function in Dart? If we remove the name of the named function then it will become a nameless or anonymous function. Besides the name of the function, it has the same features as named functions including parameters, return type, and body. Anonymous functions are also called nameless functions, inline functions, Lambda functions, and immediately-called functions.

 

What is the difference between an anonymous and non anonymous functions?

We make named functions or nonanonymous functions to use or call it multiple times but in anonymous functions, we normally call it only once. So it can be inferred that when we need to call a function for one time we make an anonymous function. We will make both to know the difference between them hereunder:-

Named/nonanonymous function

higherOrderFunction() {
   
}

 

Anonymous function

 () {
      }
}

 

Why create anonymous functions?

Named functions take namespace and resultantly application size also increases. So for code efficiency, we use anonymous functions as they will be called for only once. We can reference it as a variable or make it a Function type which is similar to other types of variables like String, int, etc. After completion of the anonymous function we use; it means it is an expression called an inline function. After that, we can give its reference to call it.

 

What is an example of an Anonymous Function?

It is pertinent to mention here that we can make an anonymous function only in a function so we will create it inside a function named “abc” with Function type reference “a”. Then we will call it in “abc” function by using its reference “a” like this:

abc(){
Function a = (int a, int b) {
  print (a+b);
};

a(10,20);

}

 

Now when we call this function in the main it will smoothly run:

void main(){
abc();
}

 

That’s how we make anonymous functions.

 

Higher Order function

The function is also a data type like int, String, bool, etc. which can be set as a parameter and while calling it we need to pass any function in it. The function wherein we set the Function data type or its return type as the Function data type is called the Higher Order Function.

 

What is Callback Function

After making a high-order function when we pass a Function as a data type that function is called a Callback function. Callback functions are triggered on events. There are two types of events i.e. System events like incoming calls, incoming messages, battery levels, and user-oriented events like onClick, onTap, onPress, onSubmit, etc.

 

Example of higher order and callback function:-

For example, we have two functions “abc” and “xyz”. In the “abc” function we will set the Function type parameter like this which is a higher order function as given hereunder:-

void abc(int a, Function b){

}

void xyz(){

}

 

Now while calling it we have to pass a function in the Function parameter. So we will pass the “xyz” function in it which will be called the callback function:

void main(){
  abc(45, xyz);
}

 

As we discussed the callback function is event-based calling so if we need to print something in the “xyz” callback function then we have to trigger it in the “abc” higher-order function as shown below:-

void abc(int a, Function b) {
b();
}
  void xyz() {
  print (“I live in Islamabad”);
  }

void main(){
  abc(45, xyz);
}

Task: Try all the above functions yourself in Dart Pad.

 

ROBINA KOUSAR
Content Writer
**************************
You can get all Mobile App Development Classes link from here:-

Related Posts

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.