×

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 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.

Type System in Dart

Dart language is a type safety enabled language that uses static and dynamic type checking to match the value of the variable with its data type at the compile-time. This...

4 minutes read.

Dart Basics

Dart, as earlier mentioned multiple times, is very similar to C, C++ and Java. It is an object-oriented, garbage collecting and class-based programming language. Let’s look further and see what Dart has...

10 minutes read.

Dart Maps

Map is an object-based data structure that associates keys and values that can be of any data type. Every key can occur only once, while the values can be used multiple...

5 minutes read.

Dart Exception Handling

Exception Class – An exception is any runtime error that leads to abrupt termination of the program. It helps in addressing the error programmatically. It is aimed to be caught...

4 minutes read.

Dart If-else-if statement

If - else - if statement enables to check the set of test expressions that evaluate to Boolean True or False, and based on this true or false the particular...

2 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.

Golang vs Dart

Go is the procedural programming language. It was founded in 2007 by Robert Griesemer, Rob Pike, and Ken Thompson at Google but was launched in 2009 as the language of...

2 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 Standard Input & Output

Standard Input in Dart (stdin): The standard input stream reads data both synchronously and asynchronously from the keyboard.  In Dart programming language, .readLineSync( ) function is used to accept input from the...

3 minutes read.

Dart - Control Flow Statements

The control flow statements are used to define the control on the flow of the program. Dart programs are executed in sequential order i.e., the order in which the programming...

1 minute read.

Dart Static Members

Static Members in Dart Static members are the members of the class declared using the 'static' keyword. the static members have the following characteristics :  All the objects of the class having...

3 minutes read.

Dart Comments

Comments are the statements that are used to enhance the readability and understandability of the code. The compiler does not execute these lines. It simply ignores these comments when it scans...

2 minutes read.

Dart Numbers

Dart provides an in-built data type ‘Numbers’ that provides two types of values: intdouble 1. int data type int data type supports integer values, and their size or values range is platform-dependent. Integers...

4 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 Generators

Synchronous Generator Dart Generator is a unique function that allows us to generate price sequences. Generators return values when needed; means that the value is generated if we try to duplicate...

5 minutes read.

C++ vs Dart

Difference between C++ and Dart C++ is an object-oriented programming language developed in 1980 by Bjarne Stroustrup. It has wide real-world applications with various in-built functions and a very vast library...

1 minute read.

Dart Classes

Dart is an object – oriented programming language that supports all the object-oriented programming concepts such as classes, objects, inheritance, data abstraction and data encapsulation. A class can be defined as...

8 minutes read.

Dart Iterable

Iterable is a collection of elements that are accessed sequentially. These elements are accessible using the iterator getter and stepping through the values using this getter. Let us understand the setting...

6 minutes read.

Dart Loops

Looping refers to repeating or reusing the same lines of code repeatedly until a particular test condition evaluates to true. It is also known as iterating.  It is effectively used when...

4 minutes read.