What is the difference between named and positional parameters?

Overview

Dart has three types of optional parameters: named, default, and positional parameters.

Optional parameters don’t require us to supply a value for the parameter when the function is called.

Also, if a caller doesn’t provide a value for an optional argument, the default value set to the parameter is utilized. This is known as a Default parameter.

What are named parameters?

The named parameters are referenced by name and declared within curly braces{ }.

The named parameters can be utilized in a different order from their declaration.

Syntax:

carSpec(String name, String model, {String color = "black"}) {
  // some code here...
}

A function can have multiple named parameters.

Note: When a function is called, the named parameters require one to specify the parameter name.

What are positional parameters?

The positional parameters are declared within a square bracket [] and can be omitted when the function is called.

Syntax:

personDetails(String name, String state, [String hobby, int age =20]){
  // some code here...
}

A function can have multiple positional parameters.

Code

// Named parameter with a default value
carSpec(String name, String model, {String color = "black"}) {
print("The $name car with the $model number was produced in 2020");
print("I like $color cars");
}
// main drive
void main() {
carSpec("Toyota Highlander", "A236C110");
}

Explanation

  • Line 2–5: We declare a function called carSpec() with the parameters name, model, and a named parameter color declared in {}. The color parameter is given a default value that is black.

  • Line 7–9: Finally, we call the carSpec() function in the main drive.

// Named parameters
carSpec(String name, String model, {String color, String year}) {
print("The $name car with the $model number was produced in $year");
print("I like $color cars");
}
// main drive
void main() {
carSpec("Toyota Highlander", "A236C110",year:"2020",color:"black");
}

Explanation

  • Line 2–5: We declare a function called carSpec() with the parameters name, model, and named parameters color and year which are declared in {}.

  • Line 7–9: Finally, we call the carSpec() function in the main drive.

Note: The named parameters (color and year) were used differently from their declaration because they are referenced by name.

// Using positional parameters
personDetails(String name, String state, [String hobby, int age =20]) {
print("$name lives in $state.");
print("She is $age years old");
print("She likes $hobby");
}
// main drive
void main() {
personDetails("Sadia", "Georgia","cooking");
}

Explanation

  • Line 2–5: We declare a function called personDetails() with the parameters name, state, and positional parameters hobby and age which are declared in []. The age parameter is given a default value of 20.

  • Line 7–9: Finally, we call the personDetails() function in the main drive.

Note: The age parameter wasn’t included when the function was called because we wanted to use the default value.

One thing to remember when using named and positional parameters is that we can’t use both at once in a function. Also, if we don’t assign a value to the parameter, it returns a null.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved