Dart Functions: Anonymous Functions or Lambda Expression

Dart Functions: Anonymous Functions or Lambda Expression

ยท

1 min read

  • In this we can create function in the main method and after that we can store this function in one variable.

  • Here , I am using myname variable to store my first function and than I use Function keyword to store my second function which is pre-defined class of dart which can used as alternative var keyword. In below example I use both var and function keyword.

  • The syntax is also same as the other function / but still there are one change that in normal function we can not put semi-column ( ; ) in end of the function , while here we have to put semi-column ( ; ) in end of the function otherwise we can face the error in our program.

  •     //Syntax
    
        (paramiter_list)
        {
    
        };
    
void main()
{
  var myname = (String name)
  {
    print("Name: $name");
  };

    Function myroll = (int roll)
  {
    print("Roll No: $roll");
  };

  myname("Vinit");
  myroll(10);
}

/** Output 
  Name: Vinit
  Roll No: 10
 */
ย