Differentiate between required and optional parameters in Dart
Differentiate between required and optional parameters in Dart
In Dart, parameters can be classified as either required or optional. Here's the difference between the two:
Example:
void printDetails(String name, int age) {
print('Name: $name, Age: $age');
}
Calling the function requires providing both name
and age
:
printDetails('John', 25);
Optional parameters can be further divided into two types: optional positional parameters and optional named parameters.
[]
[3].=
[3].int?
instead of int
)[3].Example:
String say(String from, String msg, [String? device]) {
var result = '$from says $msg';
if (device != null) {
result = '$result with a $device';
}
return result;
}
Calling the function with...
middle
Gợi ý câu hỏi phỏng vấn
Chưa có bình luận nào