Learn how to create and use functions in Dart the smart way!
In this guide, we’ll cover:
- Normal Functions
- Named Parameters (with
required) - Arrow Functions
A normal function is the standard way to define a function in Dart.
✅ You define:
- Return type (optional but recommended)
- Function name
- Parameters in parentheses
- Code block inside
{}
// Normal function example
int add(int a, int b) {
return a + b;
}
void main() {
print(add(5, 3)); // Output: 8
}- Named parameters make your function more readable.
- Pass arguments in any order.
- Wrap parameters in {}.
- Use required if a parameter must be provided.
- Use default values for optional parameters.
// Function with named parameters
void greet({required String name, int age = 0}) {
print("Hello $name, Age: $age");
}
void main() {
greet(name: "Kulani", age: 25); // Hello Kulani, Age: 25
greet(name: "Sharada"); // Hello Sharada, Age: 0
}- Arrow functions are shortcuts for single-expression functions.
- They use => instead of {} and return.
// Arrow function example
int multiply(int a, int b) => a * b;
void main() {
print(multiply(4, 5)); // Output: 20
}