What are Functions?
Functions are reusable blocks of code that perform a specific task. They help you organize your code, avoid repetition, and make it more readable and maintainable. In Dart, functions are first‑class objects, meaning they can be assigned to variables, passed as arguments, and returned from other functions.
Defining a Function
A function definition typically includes a return type, a name, a parameter list (optional), and a body. If no return type is specified, void is assumed (but it's good practice to be explicit).
Calling a Function
Function Parameters
Dart supports several kinds of function parameters: required positional parameters, optional positional parameters, and named parameters.
Required Positional Parameters
These are the most common. You must provide values for them in the order they are declared.
Optional Positional Parameters
Enclose a group of parameters in square brackets [] to make them optional. You can provide default values using =.
Named Parameters
Named parameters allow you to pass arguments by name, making the call more readable. They are enclosed in curly braces {}. By default, named parameters are optional and can be null unless marked required or given a default value.
Return Types
Functions can return a value. Specify the type before the function name. If nothing is returned, use void. You can also omit the return type, but it's not recommended.
Arrow Syntax (Fat Arrow)
For functions that consist of a single expression, you can use the arrow syntax => followed by the expression. The result of the expression is automatically returned.
Anonymous Functions (Lambdas)
You can create functions without a name. They are often used as arguments to other functions (like forEach, map, where).
Closures
A closure is a function that can access variables from its outer scope even after the outer function has finished executing. This is a powerful feature in Dart.
Lexical Scope
Dart uses lexical scoping, meaning the scope of variables is determined by the code structure. Inner functions can access variables from outer functions.
Recursion
A function can call itself. This is useful for tasks that can be broken down into similar subtasks, like calculating factorials or traversing trees.
Function as First‑Class Objects
You can assign a function to a variable, pass it as an argument, or return it from another function. This enables functional programming patterns.
Complete Example
Key Takeaways
- Functions are reusable blocks of code that may return a value.
- Parameters can be required positional, optional positional, or named (with
requiredor defaults).
- Parameters can be required positional, optional positional, or named (with
- Use arrow syntax
=>for single‑expression functions.
- Use arrow syntax
- Anonymous functions (lambdas) are useful for callbacks and functional operations.
- Closures capture variables from their enclosing scope.
- Dart functions are first‑class objects – you can assign them, pass them, and return them.
- Recursion is supported and can be elegant for certain problems.