Dart Functions: Anonymous Functions or Lambda Expression

Vinit MepaniVinit Mepani
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
 */
0
Subscribe to my newsletter

Read articles from Vinit Mepani directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Vinit Mepani
Vinit Mepani

"Hello World, I'm Vinit Mepani, a coding virtuoso driven by passion, fueled by curiosity, and always poised to conquer challenges. Picture me as a digital explorer, navigating through the vast realms of code, forever in pursuit of innovation. In the enchanting kingdom of algorithms and syntax, I wield my keyboard as a magical wand, casting spells of logic and crafting solutions to digital enigmas. With each line of code, I embark on an odyssey of learning, embracing the ever-evolving landscape of technology. Eager to decode the secrets of the programming universe, I see challenges not as obstacles but as thrilling quests, opportunities to push boundaries and uncover new dimensions in the realm of possibilities. In this symphony of zeros and ones, I am Vinit Mepani, a coder by passion, an adventurer in the digital wilderness, and a seeker of knowledge in the enchanting world of code. Join me on this quest, and let's create digital wonders together!"