Functions

Dart supports top-level functions, meaning you can define functions outside any class definition that can be called from anywhere in the code.

A Dart function is defined in the following way:

 returnType ​functionName​(parameters) {
  blockOfInstructions;
 return​ returnValue;
 }

where:

  • returnType is the data type, see the section Conversion Between Native Java/Apple and Dart Data Types, the function will return.

  • The parameters, explained in the next paragraph, which will be the data the function will use to do what it needs to do. Parameters are variables and their value is set using function call arguments. If you are not familiar with functions and function calls, the example that follows is designed to give a real, working example of a few functions that take advantage of other functions.

  • In the curly braces is where you add the block of instructions (one or more lines) which, if the function has any return type other than void, includes a return instruction, which is used to return a value to the calling code.

For example, the following function is very simple:

 double​ ​square​(​double​ n) {
 return​ n*n;
 }

but it can be used inside a function to calculate gravitational attraction, which is slightly more complex, with more parameters:

 // We'll see how to set all of those arguments soon
 double​ ​calculateGravitationalAttraction​(​double​ mass1, ​double​ mass2, ​double​ distance)
 {
 const​ G = 6.67e-11; ​// universal gravitation constant
 /*
  * Calling the square function using distance as
  * the only argument (n will be equal to
  * the value in the distance variable)
  * and saving the return value
  * to the squaredDistance variable
  */
 const​ squaredDistance = square(distance);
 return​ G*mass1*mass2/squaredDistance;
 }

and it, in turn, can be used to calculate, for example, the force of attraction between the earth and the sun, potentially wrapped inside another function:

 double​ ​calculateSunEarthAttraction​() {
 const​ earthMass = 5.97e24; ​// mass of the Earth
 const​ sunMass = 1.99e30; ​// mass of the Sun
 const​ dist = 1.496e8; ​// distance between the Earth and the Sun
 /*
  * Calling the calculateGravitationalAttraction
  * function setting the arguments one by one
  * in the order of the parameters in the
  * function's definition and returning
  * its return value to any other function
  * that needs the value of the force of
  * attraction between the Sun and the Earth
  */
 return​ calculateGravitationalAttraction(earthMass, sunMass, dist);
 }

Function Parameters

Up until now, we have only used positional parameters: you define them like variables, one after another, and you set the arguments in the same way, following the same order.

Now, though, you need to know about named parameters, which allow the calling function to set the arguments in a different order (or even for just some of the parameters) by specifying a name for each parameter. If you wrap the parameters in curly braces, you’ll make them named and optional, like in the following example:

 double​ ​calculateSunPlanetAttraction​({})

The Arrow

If the function will be simply composed of one instruction, you can use the arrow notation, so that:

 int​ ​square​(​int​ n) {
 return​ n*n;
 }

becomes:

 int​ ​square​(​int​ n) => n*n;

usually written, especially when the expression is longer, as:

 int​ ​square​(​int​ n) =>
  n*n;

Anonymous Functions and Closures

Closures are what we use to define callbacks. They are functions defined as variables or, more specifically, variables of type Function or Typedef and Callbacks.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset