Dart Functions: Named Parameter
in dart named parameters are defined within curly braces { } in a function's parameter list. which means it will be optional that whenever we call function at that it is not mandatory to declare this variable which are in curly backet , by default their value shown as null.
But , if we want to define this value then we must write parameter name before the value to get print of the output otherwise it's throw error.
I have already write both example to learn easily.
void student(var name ,{ var roll , var age})
{
print("Name = $name");
print("Roll No = $roll");
print("Age = $age");
}
void main() {
student("Vinit");
}
//Name = Vinit
//Roll No = null
//Age = null
In this we only get value of name in function at the call time , so other both files are null shown in output .
While , in the below code we can see that when we call function I have pass all three value , but I have write parameter name before the value for put value on variable .
You can see that I have also write required before roll which means I have to declare this roll value in the function at the time of call otherwise I must face error ,or I have to remove requires.
void student(var name ,{ required var roll , var age})
{
print("Name = $name");
print("Roll No = $roll");
print("Age = $age");
}
void main() {
student("Vinit" ,roll: 10 , age :20);
}
//Name = Vinit
//Roll No = 10
//Age = 20
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!"