Dung (Donny) Nguyen

Senior Software Engineer

Functions

In Dart, functions are first-class objects, meaning they can be assigned to variables, passed as arguments, and returned from other functions. This flexibility allows for functional programming patterns and concise code structures. Here’s a breakdown of function features in Dart:

Basic Syntax

A function in Dart is defined using the void keyword if it does not return a value, or by specifying the return type if it does. The syntax includes:

void main() {
  printHello();
}

void printHello() {
  print('Hello, Dart!');
}

If the function returns a value, we declare the return type:

int add(int a, int b) {
  return a + b;
}

Arrow Syntax for Single-Line Functions

Dart allows us to use a shorthand syntax called the “arrow” syntax for simple, one-line functions. Replace the curly braces and return with =>:

int multiply(int a, int b) => a * b;

Anonymous Functions (Lambdas)

Dart allows us to create functions without names, called anonymous functions or lambdas. These are useful for short callbacks:

var numbers = [1, 2, 3];
numbers.forEach((number) {
  print(number * 2);
});

Function as First-Class Objects

Since functions are first-class objects, they can be stored in variables, passed as arguments, and returned from other functions:

void executeFunction(Function callback) {
  callback();
}

void sayHello() {
  print('Hello');
}

executeFunction(sayHello);

Closures

Closures allow us to capture variables from the outer scope. Dart functions can close over variables defined outside their scope:

Function createCounter() {
  int count = 0;
  return () {
    count++;
    print(count);
  };
}

var counter = createCounter();
counter(); // 1
counter(); // 2

Asynchronous Functions

Dart supports asynchronous programming using async and await. Functions can return a Future if they perform asynchronous operations:

Future<String> fetchData() async {
  await Future.delayed(Duration(seconds: 2));
  return 'Data fetched';
}

void main() async {
  var data = await fetchData();
  print(data);
}

These features make Dart functions flexible, concise, and suitable for both simple scripts and complex applications.