×

Dart Optional Parameters

Dart functions can have optional parameters with default values. When we create a function, we can specify parameters that calling code can provide; but if the calling code chooses not to, the function uses the default values.

There are some important points to remembers :

  1. We can use optional parameters in methods and constructors.
  2. Every optional parameter contains a default value which is the part of its definition.
  3. When no value is passed to the optional arguments in the parameter, then it takes its default value.
  4. When a value is passed to the optional argument, the value passed by the user is considered over the default value.
  5. The optional parameters are always defined at the end of the parameter list.
  6. Provide a value for each optional parameter in the order in which they are declared, reading from left to right. The calling code can provide arguments to populate each of the optional parameters. Any parameters not populated default to null or the specified default value.

Optional parameters must appear in a block together after all positional parameters are defined. The optional parameters block is defined within a pair of curly brackets and is a comma-separated list.

func( int a, { int b : 5, int c : 6, int d : 7 } )

The default values for optional names parameters use a colon ( : ) to separate the parameters name and value. We can now call this function passing in the mandatory argument as following :

func( 5, b : 10, c : 11, d : 12 ) ;

func( 5, b : 10, c : 11 ) ;

func( 5, b : 10 ) ;

The calling statement must specify the parameter name for all supplied optional parameter values. The following statement is invalid :

func( 5 , 1, 11 ) ;

Consider the following code that explains the concept of optional parameters :

Example 1 :

void main( )
{
   print( " This is the code for optional parameters : " ) ;
   // calling the functions with different values being passed in parameters
   print( " \n First function call : \n " ) ;
   func( 1, b : 2 , c : 3) ;
   print( " \n Second function call : \n " ) ;
   func( 1, b : 2 ) ;
   print( " \n Third function call : \n " ) ;
   func( 1, c : 3 ) ;
   print( " \n Fourth function call : \n " ) ;
   func( 1 ) ;
}
// function definition with optional parameters
void func(int a, { int b = 5, int c = 6 } )
{
   print( "  The value of a is : $a " ) ;
   print( "  The value of b is : $b " ) ;
   print( "  The value of c is : $c " ) ;
}

Output :

 First function call : 
 
  The value of a is : 1 
  The value of b is : 2 
  The value of c is : 3 
 
 Second function call : 
 
  The value of a is : 1 
  The value of b is : 2 
  The value of c is : 6 
 
 Third function call : 
 
  The value of a is : 1 
  The value of b is : 5 
  The value of c is : 3 
 
 Fourth function call : 
 
  The value of a is : 1 
  The value of b is : 5 
  The value of c is : 6 

Related Topics

Dart Installation Guide

There are various ways of compiling and running an application created in Dart, either by compiling the Dart code to JavaScript using the Dart2js tool or by running on the...

3 minutes read.

Dart If statement

If statement is used to set the control on the lines of code. Using if statement, block of code is executed only if the expression in the statement returns true....

1 minute read.

Dart Operators Precedence and Associativity

Precedence of operators determines the order in which the operators are evaluated if they are grouped together in a sentence. If two operators share an operand then the one with...

5 minutes read.

Typedef in Dart

In Dart, typedef is used to generate a function type that we can use as a type annotation for declaring variables and return types of the function type. An alias...

4 minutes read.

Dart this Keyword

The ' this ' keyword, similar to that in Java and C#, refers to an object of the class using it. It points to the current object of the class,...

5 minutes read.

Dart Generics

Dart Generics is similar to Dart collections, which are used to store homogeneous data. As we have discussed in Dart's features, it is a language with types being optional. By default,...

4 minutes read.

Dart Type Test Operators

Type Test Operators in Dart These operators are used to check the types of expressions at runtime. Dart is a typed language, and we often want to assert that a value...

1 minute read.

Dart Objects

Apart from the built-in data types, Dart offers some other data types that have a special role to play. One of them is Objects.  Objects Objects are the foundation of the...

3 minutes read.

Common Collection Methods

Most of the programming languages have arrays as a way to list items together. However, Dart has a collection of data structures similar to array. These are supported by the...

3 minutes read.

Dart Object-Oriented Concepts

Dart is a programming language that supports object-oriented programming. It is focused on the objects that are real-world entities and supports all the concepts of OOPS, such as objects, classes,...

4 minutes read.

Rust vs Dart

Rust is a system-level programming language that stands next to C ++ in terms of syntax, but offers greater speed and memory security. Dart, on the other hand, is an...

2 minutes read.

Interfaces in Dart

An interface in Dart refers to the syntax or blueprint that any class must adhere to. It basically defines the array of methods available on the object. It provides the...

3 minutes read.

Dart Keywords

Keywords are the reserved words of a programming language, and they have a special meaning that comes defined by the language. These cannot be used as an identifier, and all...

1 minute read.

Dart - extends, with and implements Keywords

The application development in Dart programming language using the Flutter framework, regularly experience different usage of the implements, extends and with keywords. Dart has full support for inheritance that is...

5 minutes read.

Dart Future and Stream

Before understanding Future and Stream in Dart, we need first to get familiar with two terms : Synchronous and Asynchronous Programming.  In synchronous programming, a single operation is handled at a...

9 minutes read.

Dart Common Collection Methods

Most of the programming languages have arrays as a way to list items together. However, Dart has a collection of data structures similar to array. These are supported by the...

3 minutes read.

Dart Recursion

Recursion is one of the most important and interesting concepts in any programming language. It can be defined as a process in which a function calls itself directly or indirectly....

5 minutes read.

Dart Inheritance

Inheritance is a promising concept of any programming language. It is a property by which a class inherits the property of another class. Moreover, the class deriving the properties of...

5 minutes read.

Dart Queues

A queue is a user-defined collection of data. It is based on the FIFO principle ( First In First Out ) which implies that the element to be inserted first,...

3 minutes read.

Dart Miscellaneous Operators

Apart from the operators we studied so far, Dart provides some more operators which are as follows : Conditional OperatorCascade Notations Conditional Operators These are the operators that help in evaluating the expression...

1 minute read.