Title here
Summary here
Here’s a function that will take an arbitrary number of int
s as arguments.
void sum(List<int> nums) {
print(nums);
int total = 0;
for (var num in nums) {
total += num;
}
print(total);
}
void main() {
sum([1, 2]);
sum([1, 2, 3]);
var nums = [1, 2, 3, 4];
sum(nums);
}
To run the program, simply execute it using the Dart VM by invoking the dart
command.
$ dart run variadic_functions.dart
[1, 2]
3
[1, 2, 3]
6
[1, 2, 3, 4]
10
Another key aspect of Dart functions is their ability to form closures, which we’ll explore next.