Differentiate between required and optional parameters in Dart

In Dart, function parameters can be categorized into two types: required parameters and optional parameters. The main difference between these two types lies in whether you must provide a value for the parameter when calling the function.

  1. Required Parameters: Required parameters are parameters that must be provided with a value when calling a function. If a function has required parameters, you must pass the corresponding arguments when invoking the function; otherwise, the Dart analyzer will show an error.

Syntax for defining a required parameter:

ReturnType functionName(Type parameterName) {
  // Function body
}

Example of a function with required parameters:

void printName(String firstName, String lastName) {
  print('Full Name: $firstName $lastName');
}

// Calling the function with required arguments
printName('John', 'Doe'); // Output: Full Name: John Doe

In this example, the printName function has two required parameters: firstName and lastName. When calling the function, both arguments must be provided to avoid a compile-time error.

  1. Optional Parameters: Optional parameters, as the name suggests, are parameters that do not require values when calling a function. Dart allows you to mark parameters as optional by using either square brackets ([]) or curly braces ({}) around the parameter names in the function signature.
  • Positional Optional Parameters: Positional optional parameters are specified using square brackets []. They are optional and can be provided in any order when calling the function. If not provided, they will take on their default values (if defined).

Syntax for defining positional optional parameters:

ReturnType functionName(Type parameterName1, [Type parameterName2]) {
  // Function body
}

Example of a function with positional optional parameters:

void printDetails(String name, [int age, String address]) {
  print('Name: $name, Age: $age, Address: $address');
}

// Calling the function with optional arguments
printDetails('Alice'); // Output: Name: Alice, Age: null, Address: null
printDetails('Bob', 30); // Output: Name: Bob, Age: 30, Address: null
printDetails('Eve', 25, 'London'); // Output: Name: Eve, Age: 25, Address: London
  • Named Optional Parameters: Named optional parameters are specified using curly braces {}. They are optional and must be provided with their parameter names when calling the function. Named parameters allow you to provide arguments in any order and are particularly useful when a function has multiple optional parameters.

Syntax for defining named optional parameters:

ReturnType functionName(Type {parameterName1, parameterName2}) {
  // Function body
}

Example of a function with named optional parameters:

void printDetails({String name, int age, String address}) {
  print('Name: $name, Age: $age, Address: $address');
}

// Calling the function with named optional arguments
printDetails(name: 'Alice'); // Output: Name: Alice, Age: null, Address: null
printDetails(age: 30, name: 'Bob'); // Output: Name: Bob, Age: 30, Address: null
printDetails(address: 'London', name: 'Eve', age: 25); // Output: Name: Eve, Age: 25, Address: London

In this example, the printDetails function has three named optional parameters: name, age, and address. When calling the function, you can provide the arguments in any order by specifying their parameter names.

In summary, required parameters must be provided with values when calling a function, while optional parameters can be omitted or provided in different ways: positional (using square brackets) or named (using curly braces). Using optional parameters allows for more flexible function calls and enables you to define default values for parameters that are not explicitly provided.

error: Content is protected !!