Dart Functions: Optional Positional Parameters
In dart optional positional parameters are defined within square 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 square backet , by default their value shown as null.
But , if we want to define this value then we can simple declare the value in function unlike named parameter we do not need to write named before the value for declare.
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 ,and It all the value are shown without any error even I did not write named parameter before the value.
void student(var name ,[ required var roll , var age]) { print("Name = $name"); print("Roll No = $roll"); print("Age = $age"); } void main() { student("Vinit" ,10 , 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!"