Both named and positional parameters are part of optional parameter:
Optional Positional Parameters:
- A parameter wrapped by
[ ]
is a positional optional parameter.```dart getHttpUrl(String server, String path, [int port=80]) { // ... } ```
- You can specify multiple positional parameters for a function:
```dart getHttpUrl(String server, String path, [int port=80, int numRetries=3]) { // ... } ``` In the above code, `port` and `numRetries` are optional and have default values of 80 and 3 respectively. You can call `getHttpUrl` with or without the third parameter. The optional parameters are _positional_ in that you can't omit `port` if you want to specify `numRetries`.
Optional Named Parameters:
- A parameter wrapped by
{ }
is a named optional parameter.```dart getHttpUrl(String server, String path, {int port = 80}) { // ... } ```
- You can specify multiple named parameters for a function:
```dart getHttpUrl(String server, String path, {int port = 80, int numRetries = 3}) { // ... } ``` You can call `getHttpUrl` with or without the third parameter. You **must** use the parameter name when calling the function.
- Because named parameters are referenced by name, they can be used in an order different from their declaration.
```dart getHttpUrl('example.com', '/index.html', numRetries: 5, port: 8080); getHttpUrl('example.com', '/index.html', numRetries: 5); ```