In Dart, functions are a fundamental concept that allow you to group code together into reusable blocks. Named parameters are a feature of Dart functions that allow you to specify parameters by name when calling the function, providing more clarity and flexibility.
Defining function : You can define functions using the void keyword if they don't return any value, or specify the return type if they do return a value. Here's a basic function without named parameters:
void greet(String name) {
print('Hello, $name!');
}
void greet({String name, String greeting}) {
print('$greeting, $name!');
}
greet(name: 'Alice', greeting: 'Hi'); // Output: Hi, Alice!
void greet({String name = 'World', String greeting = 'Hello'}) {
print('$greeting, $name!');
}
greet(); // Output: Hello, World!
greet(name: 'Alice'); // Output: Hello, Alice!