Programming in Dart: Functions & Closures

Jun 21 2022 · Dart 2.16, Flutter, DartPad

Part 1: Meet the Function

08. Use Arrow Notation

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: 07. Understand Typedef Next episode: 09. Conclusion

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: 08. Use Arrow Notation

Often times when you will write a function that is composed of a single line of code, you can actually shorthand to make a function a single line of code. You use what is known as arrow notation. It’s also known as fat arrow syntax or lambda syntax.

Take this simple function to determine that an integer is even. Using arrow notation,

you remove the braces and return statement and replace it with an arrow. The return keyword is inferred by the arrow. Let’s put this to use.

Open up DartPad.dev in your browser. We’re going to write a function that will add two numbers together. Lets start by defining an add function.

int add(int itemA, int itemB) {
    return itemA + itemB;
}

Now in main, let’s add two numbers together in main.

void main() {
    print(add(1, 2));
}

Run the program. Now we get the result of three. Awesome. Let’s simplify the add function using arrow notation. Using arrow notation, we get rid of the braces and the return statement.

int add(int itemA, int itemB) => itemA + itemB;

We’ve distilled our function into a single line, but it runs the same way. What about functions that don’t return a value. Let’s add the following to print a message.

void log(String message) => print(message);

Now let’s use this in the main function.

void main() {
    log('this space is for rent');
}

Believe it or not, we can distill this even further.

void main() => log('this space is for rent');

Wait - things are a little strange. We are using arrow notation on void functions that don’t return a value yet the arrow notation indicates that we are returning a value. What’s going on here?

Dart is an interesting language for sure. In a language like Java, the previous examples would have failed with a compile error since you must use the return statement to return a value. Now at the very beginning of this course, I mentioned that void in Dart means the lack of a return. That is, a void function is a function that doesn’t return anything. Yet, we just defined void functions that utilize a return by way of arrow notation. In fact in the language tour guide explains that arrow notation is shorthand for a return.

So in Dart, void does not mean a lack of a return value. As the documentation explains, void indicates a value is never used. So when we converted the main function to use arrow syntax, we were getting a value back but we were disregarding it. In the case of that function, can you guess what value was returned? It was null.

Now this doesn’t mean you can define a function as void, and then return a value. This will actually produce an error. Void returning a value really comes into play with arrow notation and maybe a few other edge cases.

What happens if we remove the return type from our functions? Dart provides one for us. The function returns a dynamic instead. That is, any type but you should always define your return type.