{"id":3612,"date":"2022-05-21T06:23:27","date_gmt":"2022-05-21T06:23:27","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=3612"},"modified":"2022-05-21T06:23:27","modified_gmt":"2022-05-21T06:23:27","slug":"dart-anonymous-function","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/05\/21\/dart-anonymous-function\/","title":{"rendered":"Dart Anonymous Function"},"content":{"rendered":"\n<p>We have learned the Dart Function, which is defined by using a user-define name. Dart also provides the facility to specify a nameless function or function without a name. This type of function is known as an&nbsp;<strong>anonymous function, lambda, or closure<\/strong>. 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.<\/p>\n\n\n\n<p>We can assign the anonymous function to a variable, and then we can retrieve or access the value of the closure based on our requirement.<\/p>\n\n\n\n<p>An Anonymous function contains an independent block of the code, and that can be passed around in our code as function parameters. The syntax is as follows.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Syntax:<\/h3>\n\n\n\n<p><a href=\"https:\/\/www.javatpoint.com\/dart-anonymous-function#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-anonymous-function#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-anonymous-function#\"><\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>(parameter_list) {  \n   statement(s)  \n} <\/code><\/pre>\n\n\n\n<p>Let&#8217;s consider the following example.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example &#8211;<\/h3>\n\n\n\n<p><\/p>\n\n\n\n<p><a href=\"https:\/\/www.javatpoint.com\/dart-anonymous-function#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-anonymous-function#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-anonymous-function#\"><\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void main() {   \n  var list = &#91;\"James\",\"Patrick\",\"Mathew\",\"Tom\"];  \n  print(\"Example of anonymous function\");  \n  list.forEach((item) {  \n      print('${list.indexOf(item)}: $item');  \n});  \n}  <\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Example of anonymous function\n0: James\n1: Patrick\n2: Mathew\n3: Tom\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation:<\/h3>\n\n\n\n<p>In the above example, we defined an anonymous function with an untype argument item. The function called for each item in the list and printed the strings with its specified index value.<\/p>\n\n\n\n<p>If the function consists of one statement, then we can also write the above code in the following way.<\/p>\n\n\n\n<p><a href=\"https:\/\/www.javatpoint.com\/dart-anonymous-function#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-anonymous-function#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-anonymous-function#\"><\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>list.forEach(  \n(item) => print(\"${list.indexOf(item)}: $item\"));  <\/code><\/pre>\n\n\n\n<p>It is equivalent to the previous code. You can verify it by paste in your dart pad and run.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Lexical Scope<\/h2>\n\n\n\n<p>As we have discussed in the\u00a0Dart\u00a0introduction, it is a lexical scope language which means the variable&#8217;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&#8217;s understand the following example.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example &#8211;<\/h3>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>bool topVariable = true;  \n  \nvoid main() {  \n  var inside_Main = true;  \n \/\/ Defining Nested Function   \n   \n void myFunction() {  \n    var inside_Function = true;  \n     \n void nestedFunction() {  \n      var inside_NestedFunction = true;  \n      \/\/ This function is using all variable of the previous functions.  \n      assert(topVariable);  \n      assert(inside_Main);  \n      assert(inside_Function);  \n      assert(inside_NestedFunction);  \n    }  \n  }  \n}  <\/code><\/pre>\n\n\n\n<p>Observe the above code, the&nbsp;<strong>nestedFunction()<\/strong>&nbsp;used the variables of the previous function.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Lexical Closure<\/h2>\n\n\n\n<p>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&#8217;s scope from inner function. Let&#8217;s understand the following example.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example &#8211;<\/h3>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void main() {  \n String initial() {  \n     var name = 'Will Smith'; \/\/ name is a local variable created by init  \n    \n     void disp_Name() { \/\/ displayName() is the inner function, a closure  \n           print(name); \/\/ use variable declared in the parent function  \n  }  \n  disp_Name();  \n}  \ninit();  <\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Will Smith\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation:<\/h3>\n\n\n\n<p>In the above code, the&nbsp;<strong>initial()<\/strong>&nbsp;function created a local variable called&nbsp;<strong>name<\/strong>&nbsp;and function called&nbsp;<strong>disp_Name()<\/strong>. The&nbsp;<strong>disp_Name()<\/strong>&nbsp;function defined inside the&nbsp;<strong>initial()<\/strong>&nbsp;function and hence&nbsp;<strong>disp_Name()<\/strong>&nbsp;function has no local variable its own.<\/p>\n\n\n\n<p>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,&nbsp;<strong>initial()<\/strong>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We have learned the Dart Function, which is defined by using a user-define name. Dart also provides the facility to specify a nameless function or function without a name. This type of function is known as an&nbsp;anonymous function, lambda, or closure. An anonymous function behaves the same as a regular function, but it does not [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[918],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/3612"}],"collection":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/comments?post=3612"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/3612\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=3612"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=3612"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=3612"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}