# Dart 'Anonymous' Function(flutter)

## Introduction to Dart anonymous functions

An anonymous function is a function that does not have a name.

So far, you learned how to define named functions. If you remove the return type and name from a named function, you’ll have an anonymous function.

For example, the following defines the `add()` function that returns the sum of two integers:

```dart
int add(int x, int y) {
  return x + y;
}Code language: Dart (dart)
```

If you remove the return type (int) and name (add) from `add()` function, you’ll have the following anonymous function:

```dart
(int x, int y) {
  return x + y;
}Code language: Dart (dart)
```

Typically, you assign an anonymous function to a variable and use the variable to call the function.

For example:

```dart
void main() {
  var sum = (int x, int y) {
    return x + y;
  };

  print(sum(10, 20));
}Code language: Dart (dart)
```

Output:

```dart
30Code language: Dart (dart)
```

How it works.

First, define an anonymous function and assign it to the `sum` variable:

```dart
var sum = (int x, int y) {
  return x + y;
};Code language: Dart (dart)
```

Second, call the function via the `sum` variable and display the result:

```dart
print(sum(10, 20));Code language: Dart (dart)
```

In practice, you often pass an anonymous function to another function that accepts a function as an argument. For example:

```dart
void show(fn) {
  for (var i = 0; i < 10; i++) {
    if (fn(i)) {
      print(i);
    }
  }
}

void main() {
  // show even numbers
  show((int x) {
    return x % 2 == 0;
  });
}Code language: Dart (dart)
```

Output:

```dart
0
2
4
6
8Code language: Dart (dart)
```

How it works.

First, define a function `show()` that accepts a function (`fn`) as an argument. The `show()` function iterates the numbers from 1 to 10 and displays the number if the number makes the `fn` function `true`:

```dart
void show(fn) {
  for (var i = 0; i < 10; i++) {
    if (fn(i)) {
      print(i);
    }
  }
}Code language: Dart (dart)
```

Second, call the `show()` function and pass an anonymous function that returns true if the number is an even number:

```dart
void main() {
  // show even numbers
  show((int x) {
    return x % 2 == 0;
  });
}Code language: Dart (dart)
```

The following example illustrates how to define an anonymous function that returns another anonymous function:

```dart
void main() {
  var multiplier = (int x) {
    return (int y) {
      return x * y;
    };
  };

  var doubleIt = multiplier(2);
  print(doubleIt(10)); // 20
}Code language: Dart (dart)
```

How it works.

First, define an anonymous function and assign it to the `multiplier` variable:

```dart
var multiplier = (int x) {
  return (int y) {
    return x * y;
  };
};Code language: Dart (dart)
```

The anonymous function accepts an integer (`x`) and returns another anonymous function. The returned anonymous function accepts an integer and returns the multiplication of `x` and `y`.

Second, call the multiplier function and assign the result to the `doubleIt` variable:

```dart
 var doubleIt = multiplier(2);Code language: Dart (dart)
```

Since the `doubleIt` is a function, you can call it to double a number:

```dart
 print(doubleIt(10)); // 20Code language: Dart (dart)
```

## Summary

* An anonymous function is a function that doesn’t have a name.
    

## **THANK YOU!**
