# Functions

Domains:

## DO

### DO use a function declaration to bind a function to a name.

Modern languages have realized how useful local nested functions and closures are. It’s common to have a function defined inside another one. In many cases, this function is used as a callback immediately and doesn’t need a name. A function expression is great for that.

But, if you do need to give it a name, use a function declaration statement instead of binding a lambda to a variable.

void main() {
localFunction() {
...
}
}
void main() {
var localFunction = () {
...
};
}

## DON'T

### DON’T create a lambda when a tear-off will do.

Linter rule: unnecessary_lambdas

If you refer to a method on an object but omit the parentheses, Dart gives you a “tear-off”—a closure that takes the same parameters as the method and invokes it when you call it.

If you have a function that invokes a method with the same arguments as are passed to it, you don’t need to manually wrap the call in a lambda.

names.forEach(print);
names.forEach((name) {
print(name);
});

Page structure
Terms