Optional arguments in Dart
Tags: dart, todayilearned, beginners
Published: 2019-09-16 02:45:15

* In normal case, if you declare a function and it’s arguments, then you have to specify each argument’s value. But what we will do if we want our arguments to be optional.
    
* Optional arguments mean if you specify that argument’s value then its okay and if you don’t specify its value then also its okay. Its not mandatory to give value to optional arguments.

* Good practice says optional arguments should be placed in last at arguments list.

There are three types of optional arguments.

1. Optional Positional argument
2. optional named arguments
3. optional argument with a default value

##Optional Positional argument
***
* To specify an optional positional argument, we use [ ] square brackets.

```dart
function_name (argument1, [argument2]) {
  // statements
}
```

* In optional positional argument if we don’t pass value then it is set to NULL.

```dart
ShowMyDetails(String name, [String lastName, int age]) {
  print(name);
  print(lastName);
  print(age);
}

main() {
  ShowMyDetails("jay", "Tillu");
}

Output

jay
Tillu
null
```
* As you can see here when I don’t specify the age’s value. It is print as null.

##Optional Named Argument
***


* In optional named argument when you pass the value to argument you have to specify its name as well. That’s why it is called an optional named argument.

* Here the advantage is that as we specify the argument’s name and value, we can pass arguments in any order.
    
* To specify an optional named argument, we use {} curly braces.

####Syntax
```dart
function_name (argument1, {argument2}) {
  // statements
}
```
####The syntax for calling the function

```dart
function_name (argument_Name : value);
```

* In optional positional argument if we don’t pass value then it is set to NULL.

####Program
```dart
ShowMyDetails(String name, {String lastName, int age}) {
  print(name);
  print(lastName);
  print(age);
}

main() {
  ShowMyDetails("Jay", lastName: "Tillu", age: 24);
}

Output
Jay
Tillu
24
```

##Optional Argument with default values
***

* In optional positional argument and optional named argument, if we don’t specify the value in an argument then it is set to NULL.

* But what will happen if we want to specify our default value to those arguments? In that case, we can use an optional argument with default values.

* Here default values also can be overridden via specifying the values at function call.
    
* In short, if you don’t specify the value at function calling it will take default value, but if you specify the value at function calling it will override that new value. So here you are safe from NULL.

* To specify the optional argument with default values, we use {} curly braces.

####Syntax
```dart
function_name (argument1, {argument2 = default_value}) {
  // statements
}
```
####The syntax for calling the function

```dart
// if you want to override new value
function_name(argumentName : value); 
```

####Program

```dart
ShowMyDetails(String name,
 {String lastName = "Sanket", int age = 20}){
  print(name);
  print(lastName);
  print(age);
}

main() {
  ShowMyDetails("Jay", age: 24);
}

Output
Jay
Sanket
24
```

* Here notice that I specify Sanket as Lastname's default value. And while function calling I don’t specify any value. The compiler takes its default value and prints it.
    
* In the other hand, age has also its default value but I override that value while function calling by 24, so compiler print the new value as output.

So, guys, That’s it for optional arguments. And as I always say Please explore around it, practice it and try to understand it conceptually.

Feel free to let me know if I miss something. Till then Keep Loving, Keep Coding. And I’ll surely catch you up in next article.

Remember no teacher, no book, no video tutorial, or no blog can teach you everything. As one said Learning is Journey and Journey never ends. Just collect some data from here and there, read it, learn it, practice it, and try to apply it. Don’t feel hesitate that you can’t do that or you don’t know this concept or that concept. Remember every programmer was passed from the path on which you are walking right now. Remember Every Master was Once a Beginner. Work hard and Give your best.

###For More Information Please Visit Following Links
* [Fuchsia OS official site](https://fuchsia.dev/)
* [Dart official site](https://dart.dev/)
* [Flutter official site](https://flutter.dev/)


Jai Hind, Vande Mataram 🇮🇳

Wanna get in touch with me? Here are links. I’ll love to become your friend. 😊

- [My Site] (https://www.jaytillu.in)
- [My Blogs] (https://blogs.jaytillu.in)
- [Twitter](https://twitter.com/jay_tillu)
- [Facebook](https://www.facebook.com/jaytillu.1314/)
- [Instagram](https://www.facebook.com/jaytillu.1314/)
- [Medium](https://medium.com/jay-tillu)
- [LinkedIn](https://www.linkedin.com/in/jaytillu/)
- [Github](https://www.github.com/jay-tillu)
- [Stack overflow](https://stackoverflow.com/users/8509590/jay-tillu)
or just mail me at jd.tillu@gmail.com