Using functions in Dart
Functions are another tool for changing the control flow; a certain task is delegated to a function by calling it and providing some arguments. A function does the requested task and returns a value; the control flow returns where the function was called. In Java and C#, classes are indispensable and they are the most important structuring concept.
But Dart is both functional and object oriented. Functions are first-class objects themselves (they are of type Function) and can exist outside of a class as top-level functions (inside a class they are called methods). In prorabbits_v2.dart
of Chapter 1, Dart – A Modern Web Programming Language, calculateRabbits
is an example of a top-level function; and deposit
, withdraw
, and toString
from banking_v2.dart
of this chapter are methods, to be called on as an object of the class. Don't create a static class only as a container for helper functions!
Return types
A function can do either of the following:
- Do something...