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 functioning 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.