Programming in Dart: Functions & Closures

Jun 21 2022 · Dart 2.16, Flutter, DartPad

Part 1: Meet the Function

03. Implement Optional Parameters

Episode complete

Play next episode

Next
About this episode
Leave a rating/review
See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 02. Use Functions Next episode: 04. Understand Named Parameters

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

There are times when you want to define a parameter to a function, but that parameter might be optional.

Demo 1

To get started, head on over to DartPad. We’re going to define a function that returns a full name. It will take a first name, a last name, and a title. Of course, not many people have titles so this parameter will be an optional one.

String getFullName
String getFullName(String firstName, String lastName) {

}
[String title]
String getFullName(String firstName, String lastName, [String? title]) {
    
}
if (title != null) {
    return '$title $firstName $lastName';
} else {
    return '$firstName $lastName';
}
print(getFullName('Albert', 'Einstein', 'Professor'));
print(getFullName('Charlie', 'Chaplin'));
bool isWithinTolerance(int value) {

}
bool isWithinTolerance(int value, []) {

}
bool isWithinTolerance(int value, [int min = 0, int max = 10]) {

}
if (min <= value && value <= max) {
    return true;
} else {
    return false;
}
return min <= value && value <= max;
print(isWithinTolerance(5));
print(isWithinTolerance(15));
print(isWithinTolerance(9, 7, 11));
print(isWithinTolerance(9, 7));