Dart by Example: Optional Parameters

1. // an ordered optional parameter
2. String yell(String str, [bool exclaim = false]) {
3. var result = str.toUpperCase();
4. if (exclaim) result = result + '!!!';
5. return result;
6. }

8. // named optional parameters
9. String whisper(String str, {bool mysteriously: false}) {
10. var result = str.toLowerCase();
11. if (mysteriously) result = result + '...';
12. return result;
13. }

16. main() {
17. print(yell('Hello, World'));
18. print(yell('Hello, World', true));
19. print(whisper('Hello, World', mysteriously: true));
20. }
1. $ dart optional_params.dart
2. HELLO, WORLD
3. HELLO, WORLD!!!
4. hello, world...

by @jryanio | source | license