Parameters and Arguments
When you're building functions in Dart, you'll often need to pass data into them to make them more flexible and reusable. This is where parameters and arguments come into play!
Think of parameters as placeholders or variables that a function expects to receive, while arguments are the actual values you pass to the function when calling it.
What are Parameters?
Parameters are variables defined in a function's signature that act as placeholders for values that will be passed to the function.
void greetUser(String name) { // 'name' is a parameter
print('Hello, $name!');
}
What are Arguments?
Arguments are the actual values you pass to a function when you call it.
greetUser('Alice'); // 'Alice' is an argument
Types of Parameters in Dart
1. Required Positional Parameters
These are the most basic type - you must provide them in the exact order they're defined.
void introduce(String name, int age, String city) {
print('Hi, I\'m $name, $age years old, from $city');
}
// Must provide all arguments in order
introduce('Bob', 25, 'New York');
2. Optional Positional Parameters
Wrap parameters in square brackets [] to make them optional. You can provide default values too!
void greet(String name, [String greeting = 'Hello']) {
print('$greeting, $name!');
}
// Both calls are valid
greet('Charlie'); // Uses default greeting
greet('Diana', 'Hi'); // Uses custom greeting
3. Named Parameters
Wrap parameters in curly braces {} to make them named. This makes function calls more readable!
void createUser({String name = '', int age = 0, String email = ''}) {
print('User: $name, Age: $age, Email: $email');
}
// You can call with any order and skip optional ones
createUser(name: 'Eve', email: '[email protected]');
createUser(age: 30, name: 'Frank');
4. Required Named Parameters
Add the required keyword to make named parameters mandatory.
void bookFlight({required String destination, required DateTime date, String? seatType}) {
print('Flight to $destination on $date');
if (seatType != null) {
print('Seat type: $seatType');
}
}
// Must provide required parameters
bookFlight(destination: 'Paris', date: DateTime.now());
Mixing Parameter Types
You can combine different parameter types, but follow this order:
- Required positional parameters
- Optional positional parameters
- Named parameters
void complexFunction(String required, [String? optional], {String named = 'default'}) {
print('Required: $required');
print('Optional: ${optional ?? 'not provided'}');
print('Named: $named');
}
// Valid calls
complexFunction('test');
complexFunction('test', 'optional value');
complexFunction('test', 'optional value', named: 'custom');
Pro Tips
- Use positional parameters for essential data that always makes sense in a specific order
- Use named parameters when you have many parameters or when the meaning isn't obvious from position
- Provide sensible default values to make your functions easier to use
- Use the
requiredkeyword for named parameters that are essential
Parameters and arguments are fundamental to writing clean, reusable code. They allow you to create flexible functions that can work with different data while keeping your code DRY (Don't Repeat Yourself)!