Dart Functions: Default Optional Parameter


In dart Default Optional Parameter means that if we do not need to print null value than we can define the value in function , so whenever we do not call the value in main function than by default pre-define value will be print which we have define in function at the time of creation. this method we can use in Named parameter and Optional parameter.
And . if we define the value in main function than the function call the value which is define in the main function and ignore the default value which is ser by default.
I have already write both example to learn easily.
//Named Parameter
void student(var name ,{ var roll })
{
print("Name = $name");
print("Roll No = $roll");
}
// Optional Parameter
void student1(var name ,[ var roll ])
{
print("Name = $name");
print("Roll No = $roll");
}
void main() {
student("Vinit");
student1("Jay");
}
Name = Vinit
Roll No = null
Name = Jay
Roll No = null
In above example we can see that we use both name and optional parameter and we did not pass any value of this in main function , hence we can see output as null.
In below example we use by default value.
```dart //Named Parameter void student(var name ,{ var roll = 2 })
{ print("Name = $name"); print("Roll No = $roll");
}
// Optional Parameter void student1(var name ,[ var roll = 2 ])
{ print("Name = $name"); print("Roll No = $roll");
}
void main() { student("Vinit"); student1("Jay");
}
Name = Vinit Roll No = 1 Name = Jay Roll No = 2 ```
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!"