Watch this episode for free — or access the full course as a raywenderlich.com Professional Subscriber. Learn more here
Programming in Dart: Functions & Closures
Part 1: Meet the Function Implement Optional Parameters
— Flutter & Dart

Lesson Complete

Play Next Lesson
Next

Implement Optional Parameters

This video was last updated on Jun 21 2022

Make your functions easier to use by providing options to the function caller. Of course, doing so requires you to write your parameters in a special way.

Contributors

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

For example, you might provide an address function that provides a parameter for a PO Box. Most people don’t have one, so instead of requiring the caller to pass in dummy data, you define the parameter in a way that caller doesn’t even need to include it.

We define optional parameters by wrapping them with brackets, much like a list. When we surround the parameters in brackets, the variable becomes optional since it may or may not contain a value. Let’s define our own optional parameter now.

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.

First, let’s define the return type and the function name.

String getFullName

Now we define the next two parameters which is the first name and the last name.

String getFullName(String firstName, String lastName) {

}

Now lets define the title. I’m going to give you a moment. See if you can do it. Okay, remember, we surround it with brackets.

[String title]

You’ll notice the title gives us an error. This is because the title should be an optional. Make the string an optional.

String getFullName(String firstName, String lastName, [String? title]) {
    
}

That makes the error go away. Of course, we’re still getting an error from the function because we haven’t finished it yet. Let’s fill out the rest.

if (title != null) {
    return '$title $firstName $lastName';
} else {
    return '$firstName $lastName';
}

Here we check if the title is null and if it isn’t, we add the title. Otherwise, we print out the name.

Now, let’s call our function in main.

print(getFullName('Albert', 'Einstein', 'Professor'));

When we run the function, we get the name printed to the console. Now, let’s call the function again, skipping the title.

print(getFullName('Charlie', 'Chaplin'));

Run again, and this time, the function skips the title.

Sometimes, you’ll want to define an optional parameter but provide a default value that isn’t null but rather some other value. That way, if the parameter isn’t used, it will still have a value. When you provide a default value, the parameters in question aren’t optional since they will always have a value. Either one provided by the caller, or one provided by the function itself. Lets add some default values to our parameters.

Back in DartPad, we’re going to define a new function to check whether a value is within a certain tolerance. For instance, you a set a minimum value of seventy and a maximum values of one hundred. Then you check to see if a number is between seventy and a hundred. If it is, that number is within tolerance.

Let’s define our function.

bool isWithinTolerance(int value) {

}

Here we defined a simple function that takes in our value. Now lets add our min and max values. The min will be zero and the max will be ten. Start by putting the optional parameters in the braces.

bool isWithinTolerance(int value, []) {

}

Now lets add our values.

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

}

Perfect. Now all we have to do is check the value.

if (min <= value && value <= max) {
    return true;
} else {
    return false;
}

We actually distill this down even further to return the result of the expression.

return min <= value && value <= max;

That’s all there is to it. Now let’s check to see if five is within tolerance. Add the following to main.

print(isWithinTolerance(5));

This turns out to be true. Let’s check the number 15.

print(isWithinTolerance(15));

And this is not in tolerance. Now we can define our own min and max values. We’ll determine if the number nine is between seven and eleven. Run the program. And it is.

print(isWithinTolerance(9, 7, 11));

We can also provide just a different minimum value.

print(isWithinTolerance(9, 7));

Now, the minimum value is seven and the max value is still ten so nine is within tolerance. Unfortunately, we can’t just set a max value. Not only that, looking at the functions, it’s not clear the meaning of the numbers being passed into the function. Don’t worry. We have a way to make it clear which number does what just from looking at it. And you’ll learn how to do it in the next episode.

Reviews

Comments