How to write functions
Functions are the basic building blocks of any programming language and Dart is no different. The basic structure of a function is as follows:
optionalReturnType functionName(optionalType parameter1, optionalType parameter2...) {
// code
}
You have already written a few functions in previous recipes. In fact, you really can't write a proper Dart application without them.
Dart also has some variations of this classical syntax and provides full support for optional parameters, optionally named parameters, default parameter values, annotations, closures, generators, and asynchronicity decorators. This may seem like a lot to cover in one recipe, but with Dart, most of this complexity will disappear.
Let's explore how to write functions and closures in this recipe.
Getting ready
- To follow along with this recipe, you can write the code in DartPad. Begin with a clean Pad, with an empty
main
method.
How to do it...
We'll continue with the same pattern...