Defining Functions
In Dart, functions are first-class objects. You can assign them to variables or pass them as arguments to other functions.
dart
Wait! For functions that only contain one expression, you can use the Arrow Syntax (
=>) to make them much shorter.dart
Parameters
Dart has two types of parameters:
- Positional: Required by default, passed in a specific order.
- Named: Optional by default, passed using names inside
{}.
dart
Required Named Parameters
If you want a named parameter to be mandatory, use the
required keyword.dart
Anonymous Functions (Lambdas)
You can create functions without a name. These are often used as callbacks (e.g., in
onPressed handlers in Flutter).dart
Summary
| Term | Description |
|---|---|
| void | Function that returns nothing |
| => | Shorthand for single-expression functions |
| { } | Syntax for named parameters |
| [ ] | Syntax for optional positional parameters |
| required | Making a named parameter mandatory |
| Best For | Reusability and organizing logic |
| Logic | "Functions are data" |
| Safety | Strong type checking for inputs and outputs |
| Modern | Concise arrow syntax for simple tasks |
| Standard | Use lowerCamelCase for function names |
| Identity | Functions can be defined at the "top level" of a file |