Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL SQLITE MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING HTML & CSS BASH RUST DART PLAYWRIGHT TOOLS MATH SHEETS POWER BI

Dart Tutorial

Dart HOME Dart Intro Dart Get Started Dart Syntax Dart Output Dart Comments Dart Variables Dart final and const Dart Data Types Dart Numbers Dart Strings Dart String Interpolation Dart String Methods Dart Booleans Dart Type Conversion Dart Null Safety Dart late Dart Operators Dart Arithmetic Dart Assignment Dart Comparison Dart Logical Dart Type Test Dart Null Operators Dart Cascade Dart If...Else Dart Switch Dart While Loop Dart For Loop Dart Break/Continue

Dart Collections

Dart Lists Dart Access List Items Dart Change List Items Dart Loop Lists Dart List Methods Dart Sets Dart Maps Dart Access Map Items Dart Loop Maps Dart Collection if/for Dart Spread Operator

Dart Functions

Dart Functions Dart Parameters Dart Named Parameters Dart Return Values Dart Arrow Functions Dart Anonymous Functions Dart Closures Dart Scope Dart Recursion

Dart Classes

Dart OOP Dart Classes/Objects Dart Constructors Dart Named Constructors Dart Factory Constructors Dart Getters and Setters Dart Static Members Dart Inheritance Dart Abstract Classes Dart Interfaces Dart Mixins Dart Extension Methods Dart Enums Dart Generics

Dart 3 Features

Dart Records Dart Patterns Dart Switch Expressions Dart Sealed Classes Dart Class Modifiers

Dart Errors

Dart Exceptions Dart Custom Exceptions

Dart Async

Dart Async Intro Dart Futures Dart async/await Dart Streams Dart Isolates

Dart Libraries

Dart Imports Dart Packages Dart Math Dart Dates Dart JSON Dart RegExp Dart User Input Dart Files Dart Testing

Dart Examples

Dart Examples Dart Exercises


Dart Parameters


Share

Dart Parameters

Information can be passed to a function as parameters.

You write the parameters inside the parentheses, after the function name.

Each parameter has a type and a name.

This function has one parameter, called name, of type String:

Example

void greet(String name) {
  print('Hello, $name!');
}

void main() {
  greet('Tom');
  greet('Anna');
  greet('Linus');
}
Try it Yourself »

Result:

Hello, Tom!
Hello, Anna!
Hello, Linus!

Note: The variable in the function definition is called a parameter.

The value you send when you call the function is called an argument.

In the example above, name is a parameter, and 'Tom' is an argument.


Multiple Parameters

You can have as many parameters as you want, separated by commas:

Example

void showPerson(String name, int age) {
  print('$name is $age years old');
}

void main() {
  showPerson('Tom', 30);
  showPerson('Anna', 25);
}
Try it Yourself »

Result:

Tom is 30 years old
Anna is 25 years old

These are called positional parameters, because the arguments are matched to the parameters by their position.

The first argument goes to the first parameter, the second argument goes to the second parameter, and so on.



Required Parameters

Normal positional parameters are required.

You must send exactly one argument for each of them, with the right type.

If you send too few arguments, the program does not run:

Example

void showPerson(String name, int age) {
  print('$name is $age years old');
}

void main() {
  showPerson('Tom');
}
Try it Yourself »

Result:

main.dart:6:13: Error: Too few positional arguments: 2 required, 1 given.
  showPerson('Tom');
            ^
main.dart:1:6: Context: Found this candidate, but the arguments don't match.
void showPerson(String name, int age) {
     ^^^^^^^^^^

The type is checked too, so you cannot send a String where an int is expected:

Example

void showPerson(String name, int age) {
  print('$name is $age years old');
}

void main() {
  showPerson('Tom', 'thirty');
}
Try it Yourself »

Result:

main.dart:6:21: Error: The argument type 'String' can't be assigned to the parameter type 'int'.
  showPerson('Tom', 'thirty');
                    ^

Optional Positional Parameters

To make a positional parameter optional, put it inside square brackets [].

Optional parameters must come after the required ones.

An optional parameter that you do not send is null, so its type must be nullable, like String?:

Example

void greet(String name, [String? title]) {
  if (title != null) {
    print('Hello, $title $name!');
  } else {
    print('Hello, $name!');
  }
}

void main() {
  greet('Smith', 'Dr.');
  greet('Anna');
}
Try it Yourself »

Result:

Hello, Dr. Smith!
Hello, Anna!

Default Parameter Values

You can give an optional parameter a default value with =.

The default value is used when no argument is sent.

Because the parameter always has a value, its type does not need to be nullable:

Example

void myCountry([String country = 'Norway']) {
  print('I am from $country');
}

void main() {
  myCountry('Sweden');
  myCountry('India');
  myCountry();
  myCountry('Brazil');
}
Try it Yourself »

Result:

I am from Sweden
I am from India
I am from Norway
I am from Brazil

Here is a function with one required parameter and two optional parameters with default values:

Example

void order(String item, [int quantity = 1, String size = 'medium']) {
  print('$quantity x $item ($size)');
}

void main() {
  order('coffee');
  order('tea', 2);
  order('pizza', 3, 'large');
}
Try it Yourself »

Result:

1 x coffee (medium)
2 x tea (medium)
3 x pizza (large)

Optional positional parameters are filled from left to right.

You cannot skip quantity and only send size.

When you want to send only some of the optional values, use named parameters instead.

Note: A default value must be a constant, like a number, a string or true.



Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by W3Schools. All Rights Reserved. W3Schools is Powered by W3.CSS.