> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/NormandoRamirezDelgado/6C-Febrero-2026/llms.txt
> Use this file to discover all available pages before exploring further.

# Optional Parameters

> Learn how to use optional parameters in Dart functions

## Introduction

Optional parameters allow you to call functions with or without providing all arguments. This makes functions more flexible when some values aren't always needed.

## Optional Parameter Syntax

Wrap optional parameters in square brackets `[]` and provide default values:

```dart theme={null}
void mostrarInfo(String nombre, [String ciudad = 'Sin Ciudad']) {
  print('Nombre: $nombre, Ciudad: $ciudad');
}
```

### Key Components

* **Required parameter**: `String nombre` - must always be provided
* **Square brackets**: `[]` - indicate optional parameters
* **Default value**: `= 'Sin Ciudad'` - used when the parameter is not provided

## Using Optional Parameters

### With Default Value

When you don't provide the optional parameter, the default value is used:

```dart theme={null}
mostrarInfo('Juan');
// Output: Nombre: Juan, Ciudad: Sin Ciudad
```

### With Provided Value

When you provide the optional parameter, your value overrides the default:

```dart theme={null}
mostrarInfo('Ana', 'Ciudad de México');
// Output: Nombre: Ana, Ciudad: Ciudad de México
```

## Complete Example

```dart theme={null}
// Parámetros opcionales entre []
void mostrarInfo(String nombre, [String ciudad = 'Sin Ciudad']) {
  print('Nombre: $nombre, Ciudad: $ciudad');
}

void main() {
  mostrarInfo('Juan');                    // Ciudad: Sin Ciudad
  mostrarInfo('Ana', 'Ciudad de México');  // Ciudad: Ciudad de México
}
```

## Multiple Optional Parameters

You can have multiple optional parameters:

```dart theme={null}
void crearPerfil(
  String nombre, 
  [String ciudad = 'Desconocida', 
   int edad = 0,
   String pais = 'México']
) {
  print('Nombre: $nombre');
  print('Ciudad: $ciudad');
  print('Edad: $edad');
  print('País: $pais');
}

void main() {
  crearPerfil('Carlos');
  crearPerfil('Ana', 'Guadalajara');
  crearPerfil('Luis', 'Monterrey', 30);
  crearPerfil('María', 'Puebla', 25, 'México');
}
```

<Note>
  Optional positional parameters must be provided in order. You cannot skip an optional parameter and provide a later one.
</Note>

## Default Values

### String Defaults

```dart theme={null}
void saludar([String mensaje = 'Hola']) {
  print(mensaje);
}
```

### Numeric Defaults

```dart theme={null}
void calcular([int cantidad = 1, double precio = 0.0]) {
  print('Total: ${cantidad * precio}');
}
```

### Boolean Defaults

```dart theme={null}
void configurar([bool activo = true]) {
  print('Estado: ${activo ? "Activo" : "Inactivo"}');
}
```

## Nullable Optional Parameters

You can make optional parameters nullable without default values:

```dart theme={null}
void mostrarDatos(String nombre, [String? apellido]) {
  if (apellido != null) {
    print('Nombre completo: $nombre $apellido');
  } else {
    print('Nombre: $nombre');
  }
}

void main() {
  mostrarDatos('Juan');
  mostrarDatos('Ana', 'García');
}
```

<Tip>
  Use the `?` suffix to make a parameter nullable when you don't want to provide a default value.
</Tip>

## Optional vs Required

<CardGroup cols={2}>
  <Card title="Required Parameters" icon="asterisk">
    * Must be provided when calling the function
    * Come before optional parameters
    * No square brackets needed

    ```dart theme={null}
    void func(String required) { }
    ```
  </Card>

  <Card title="Optional Parameters" icon="circle-question">
    * Can be omitted when calling the function
    * Come after required parameters
    * Enclosed in square brackets

    ```dart theme={null}
    void func([String? optional]) { }
    ```
  </Card>
</CardGroup>

## When to Use Optional Parameters

<Check>
  * When a parameter has a sensible default value
  * When some function features are only needed occasionally
  * To maintain backward compatibility when adding new parameters
  * To reduce the number of function overloads
</Check>

## Best Practices

<Warning>
  * Always provide meaningful default values
  * Don't create too many optional parameters (3 or fewer is ideal)
  * Consider using named parameters instead if you have many optional parameters
  * Document what the default values mean
</Warning>

<Tip>
  If you need to skip optional parameters in the middle, consider using named parameters instead of positional optional parameters.
</Tip>

## Comparison Table

| Aspect    | Required    | Optional Positional     |
| --------- | ----------- | ----------------------- |
| Syntax    | `Type name` | `[Type name = default]` |
| Order     | Must match  | Must match              |
| Skippable | No          | No (without later ones) |
| Default   | None        | Required                |
| Nullable  | With `?`    | With `?` or default     |
