Optional Arguments in Dart

Optional Arguments in Dart

In Dart, Optional arguments mean if you specify that argument’s value then it's okay and if you don’t specify its value then also okay.

·

6 min read

  • In normal case, if you declare a function and its 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 it's okay and if you don’t specify its value then also okay. It's not mandatory to give value to optional arguments.

  • Good practice says optional arguments should be placed in the last arguments list.

There are three types of optional arguments.

  1. Optional Positional argument

  2. optional named arguments

  3. an optional argument with a default value

Optional Positional argument

  • To specify an optional positional argument, we use [ ] square brackets.

Syntax

function_name (argument1, [argument2]) {
  // statements
}
  • In the optional positional argument if we don’t pass the value then it is set to NULL.

Sample Program

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 I don’t specify the age’s value. It is printed 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

function_name (argument1, {argument2}) {
  // statements
}

The syntax for calling the function

function_name (argument_Name : value);
  • In the optional positional argument if we don’t pass the value then it is set to NULL.

Sample Program

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 by specifying the values at the function call.

  • In short, if you don’t specify the value at function calling it will take the 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

function_name (argument1, {argument2 = default_value}) {
  // statements
}

The syntax for calling the function

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

Sample Program

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.

  • On the other hand, age has also its default value but I override that value while the function is called by 24, so the compiler prints the new value as output.

Conclusion

In Dart, optional arguments provide flexibility and convenience in defining functions. They allow us to create functions where certain parameters can be omitted while calling the function, and default values can be specified for those parameters.

We explored three types of optional arguments:

  1. Optional Positional Argument: Using square brackets [], we can define optional positional arguments. If the value for such an argument is not provided during the function call, it is automatically set to null. These arguments should be placed at the end of the argument list to maintain consistency and avoid ambiguity.

  2. Optional Named Argument: By using curly braces {}, we can create optional named arguments. With named arguments, we can pass values to the function based on the argument names, allowing us to change the order of arguments during the function call. Like optional positional arguments, if a value is not provided for a named argument, it defaults to null.

  3. Optional Argument with Default Values: In this approach, we can set default values for optional arguments using the equal sign =. These default values are used when the function is called without specifying a value for the respective argument. If a new value is provided during the function call, it overrides the default value.

By understanding these three types of optional arguments, developers can create more flexible and expressive functions. They help in reducing the number of overloaded functions and make the code more readable. However, it's essential to use optional arguments judiciously and consider the context and requirements of the application.

In conclusion, optional arguments enhance the power of user-defined functions in Dart, allowing developers to design more versatile and adaptable functions. When used correctly, optional arguments can improve the clarity and maintainability of the codebase, making it easier to work with functions in Dart programming. So, make the most of optional arguments and elevate your Dart coding experience to new heights. Happy coding!

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 the following 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 hesitant 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.

Learn More about Dart and Flutter

Follow me for more such content.

Did you find this article valuable?

Support Jay Tillu by becoming a sponsor. Any amount is appreciated!