Programming in Dart: Functions & Closures

Jun 21 2022 · Dart 2.16, Flutter, DartPad

Part 1: Meet the Function

04. Understand Named 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: 03. Implement Optional Parameters Next episode: 05. Challenge: Create a Function

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.

Transcript: 04. Understand Named Parameters

So far in defining functions, you will have noticed that often times, it’s difficult to determine the meaning of the value being passed into it.

Let’s look at our last example. Tell me, just looking at the code sample, can you tell me which is value parameter, the minimum parameter and the maximum parameter? You may be able to guess, but you’d have to check the documentation or source code to be sure.

Now before I go any further, I need to define a little terminology.

As mentioned, when you define a series of variables passed into a function, you call them parameters. Nothing new here. Yet, when you call that function and pass values into that function,

those values being used are known as arguments. This is a common source of confusion so don’t be surprised if you meet someone who says argument instead of parameter or vice versa.

Okay back to our function call. Thankfully, one nice feature with dart is give parameters names when the function is used. By default, parameters are nameless which makes it hard to determine the meaning of the parameter.

By using named parameters, you can tell at glance the reasoning by the parameters. When you work with Flutter to create a new widget, you’re using named parameters. IOkay let’s get to work and create some named parameters. Just one thing to note - if you use named parameters, they either must be an optional, contain a default value, or require a value.

To get started, open up a browser and head on over to DartPad.dev. In the last episode, we defined a function to check the tolerance of a number. That is, whether a number is between the minimum and maximum. Let’s add that function now.

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

Okay, we’ve defined a function that requires a value but has default values for a minimum and a maximum. Now let’s make them named parameters. We do this by putting parameters in braces.

bool isWithinTolerance(int value, {int min = 0, int max = 10}) {
  return min <= value && value <= max;
}

Now, let’s call the function.

print(isWithinTolerance(9, min: 7, max: 11));

As you can see, by using named parameters, it is pretty clear. We can also see set a default value to the max while leaving out the min value.

print(isWithinTolerance(9, max: 11));

This is something you couldn’t do with unnamed parameters. You can also use your own order.

print(isWithinTolerance(9, max: 11, min: 5));

Of course, since the parameters are optional, you can just drop them as well.

print(isWithinTolerance(9));

Now lets make all of them named parameters so our function is easy to read.

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

And right away, we get an error because remember - we must either provide a default value or designate it an optional. Yet, we don’t know the value. So, we’re going to require a value instead.

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

The error goes away because now we require a value from the caller. Yet, now are calling code is showing an error. This is because we are using a named parameter, so let’s update the code to use it.

print(isWithinTolerance(value: 9, max: 11, min: 5));

And now it works! Named parameters may seem verbose but when you start working with lots and lots of parameters, you’ll really appreciate them.