Dart Anonymous Functions

Summary: in this tutorial, you’ll learn how to define Dart anonymous functions which are functions that have no names.

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:

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:

(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. Note that functions are first-class citizens in Dart.

For example:

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

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

Output:

30Code language: Dart (dart)

How it works.

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

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:

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:

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:

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:

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:

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:

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:

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:

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

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

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

Summary

  • An anonymous function is a function that doesn’t have a name.
Was this tutorial helpful ?