Junior — Middle
What is the role and advantages of using named parameters in functions or methods?
sobes.tech AI
Answer from AI
Named parameters in functions or methods allow explicitly specifying which parameter corresponds to the passed value. This enhances the readability and usability of functions, especially when there are many parameters or they have default values.
Advantages:
- Improved code readability — it's immediately clear which parameter is being passed.
- Ability to change the order of arguments when calling.
- Reduced errors when passing parameters.
- Support for parameters with default values.
Example in Dart (Flutter):
void greet({String name = 'Guest', int age = 0}) {
print('Hello, $name! You are $age years old.');
}
// Calling with named parameters
greet(name: 'Anna', age: 25);
greet(age: 30); // name will be 'Guest'
Thus, named parameters make the code more understandable and flexible.