> ## 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.

# Arrow Functions

> Learn how to use arrow function syntax in Dart

## Introduction

Arrow functions (also called fat arrow syntax) provide a concise way to write functions that contain a single expression. They're perfect for simple operations and callbacks.

## Arrow Function Syntax

Use the `=>` operator to create a single-expression function:

```dart theme={null}
int multiplicarFlecha(int a, int b) => a * b;
```

### Key Components

* **Return type**: `int` - same as traditional functions
* **Function name**: `multiplicarFlecha`
* **Parameters**: `(int a, int b)` - same as traditional functions
* **Arrow operator**: `=>` - replaces the curly braces and return keyword
* **Expression**: `a * b` - automatically returned

## Traditional vs Arrow Syntax

### Traditional Function

```dart theme={null}
int multiplicar(int a, int b) {
  return a * b;
}
```

### Arrow Function

```dart theme={null}
int multiplicarFlecha(int a, int b) => a * b;
```

<Note>
  Both functions do exactly the same thing, but the arrow function is more concise.
</Note>

## Complete Example

```dart theme={null}
// Forma tradicional
int multiplicar(int a, int b) {
  return a * b;
}

// Forma flecha (para una sola línea)
int multiplicarFlecha(int a, int b) => a * b;

void main() {
  print(multiplicar(4, 5));  // Salida: 20

  //Llamar la Función de flecha o Arrow Functions
  print(multiplicarFlecha(4, 5));  // Salida: 20
}
```

## When to Use Arrow Functions

<Check>
  Arrow functions are ideal for:

  * Single-expression functions
  * Simple calculations
  * Getters and simple methods
  * Callback functions
  * Mapping and filtering operations
</Check>

## Common Use Cases

### Mathematical Operations

```dart theme={null}
int sumar(int a, int b) => a + b;
int restar(int a, int b) => a - b;
double dividir(int a, int b) => a / b;
int modulo(int a, int b) => a % b;
```

### String Operations

```dart theme={null}
String saludar(String nombre) => 'Hola, $nombre';
String mayusculas(String texto) => texto.toUpperCase();
int longitud(String texto) => texto.length;
```

### Boolean Operations

```dart theme={null}
bool esPar(int numero) => numero % 2 == 0;
bool esPositivo(int numero) => numero > 0;
bool esVacio(String texto) => texto.isEmpty;
bool esMayor(int a, int b) => a > b;
```

### Void Functions

Arrow functions work with void return types too:

```dart theme={null}
void imprimir(String mensaje) => print(mensaje);
void saludar() => print('Hola mundo');
void despedir(String nombre) => print('Adiós, $nombre');
```

## Arrow Functions with Named Parameters

```dart theme={null}
int calcular({
  required int a,
  required int b,
  required String operacion
}) => operacion == 'suma' ? a + b : a - b;

void main() {
  print(calcular(a: 10, b: 5, operacion: 'suma'));  // 15
  print(calcular(a: 10, b: 5, operacion: 'resta')); // 5
}
```

## Arrow Functions in Collections

### With Lists

```dart theme={null}
void main() {
  var numeros = [1, 2, 3, 4, 5];
  
  // Map with arrow function
  var dobles = numeros.map((n) => n * 2);
  print(dobles.toList());  // [2, 4, 6, 8, 10]
  
  // Filter with arrow function
  var pares = numeros.where((n) => n % 2 == 0);
  print(pares.toList());  // [2, 4]
}
```

### With forEach

```dart theme={null}
void main() {
  var nombres = ['Ana', 'Carlos', 'Diana'];
  
  // Traditional
  nombres.forEach((nombre) => print('Hola, $nombre'));
  
  // Even more concise
  nombres.forEach(print);
}
```

## Limitations of Arrow Functions

<Warning>
  Arrow functions can only contain a single expression. You cannot:

  * Use multiple statements
  * Declare local variables
  * Use control flow statements (if, for, while)
  * Have multiple lines of code
</Warning>

### Won't Work

```dart theme={null}
// ❌ Error: Multiple statements
int calcular(int a, int b) => {
  int resultado = a + b;
  return resultado;
};

// ❌ Error: Control flow
int absoluto(int n) => {
  if (n < 0) return -n;
  return n;
};
```

### Will Work

```dart theme={null}
// ✅ Single expression with ternary
int absoluto(int n) => n < 0 ? -n : n;

// ✅ Single expression
int calcular(int a, int b) => a + b;
```

## Getters with Arrow Syntax

Arrow functions are commonly used for getters:

```dart theme={null}
class Rectangulo {
  final double ancho;
  final double alto;
  
  Rectangulo(this.ancho, this.alto);
  
  // Arrow function getter
  double get area => ancho * alto;
  double get perimetro => 2 * (ancho + alto);
  bool get esCuadrado => ancho == alto;
}

void main() {
  var rect = Rectangulo(5, 10);
  print(rect.area);       // 50.0
  print(rect.perimetro);  // 30.0
  print(rect.esCuadrado); // false
}
```

## Comparison Chart

<CardGroup cols={2}>
  <Card title="Traditional Functions" icon="code">
    **Use when:**

    * Multiple statements needed
    * Complex logic required
    * Local variables needed
    * Better for debugging

    ```dart theme={null}
    int func(int x) {
      var result = x * 2;
      return result + 1;
    }
    ```
  </Card>

  <Card title="Arrow Functions" icon="arrow-right">
    **Use when:**

    * Single expression
    * Simple calculations
    * Callbacks
    * Conciseness matters

    ```dart theme={null}
    int func(int x) => x * 2 + 1;
    ```
  </Card>
</CardGroup>

## Best Practices

<Tip>
  * Use arrow functions for simple, single-expression operations
  * Keep expressions short and readable
  * Don't sacrifice readability for brevity
  * Use traditional syntax when logic becomes complex
  * Perfect for functional programming patterns
</Tip>

<Check>
  **Good arrow function examples:**

  * Getters and setters
  * Simple calculations
  * Callbacks and lambdas
  * Mapping and filtering
  * Converting or transforming data
</Check>

## Performance Note

<Note>
  Arrow functions and traditional functions have the same performance. The arrow syntax is purely syntactic sugar for writing more concise code.
</Note>
