DMarlon Studio

Lesson 13 · 13 of 20

Functions and arrow syntax · Dart

Write Dart functions with a return type, return and the => arrow syntax, use void, and store functions in variables: a temperature converter.

Dart · Functions and arrow syntax in DMarlon Studio: the example's code and its output in the terminal

A function groups code under a name so you can use it as often as you need. double toFahrenheit(double celsius) says what it returns (double), what it is called and what parameter it takes. Inside, `return` hands back the result: celsius * 9 / 5 + 32.

When a function is a single expression, the arrow syntax shortens it: bool isFreezing(double celsius) => celsius <= 0; is the same as a block with return. Functions call each other: describe uses isFreezing and the conditional operator to give freezing, hot or mild.

`void` means the function returns nothing: report does a job, printing a line, and that is it. You pass the arguments in the same order as the parameters: report('Oslo', -4). The -4 is an integer, but Dart turns it into a double because the parameter asks for one, hence -4.0.

A function's result can be stored in a variable or used straight inside ${}. And functions are values too: var convert = toFahrenheit; stores the function without calling it, and (int n) => n * n is an anonymous function, with no name, stored in square. The coming lessons use them a lot.

In Flutter everything is built with functions: buttons take a function that runs when they are tapped. In DMarlon Studio, the code that runs when you tap a button on the phone canvas lives in a function like these.

Code · main.dart

// A function: return type, name, parameters and a block.double toFahrenheit(double celsius) {  return celsius * 9 / 5 + 32;}// Arrow syntax (=>) for a function that is a single expression.bool isFreezing(double celsius) => celsius <= 0;String describe(double celsius) =>    isFreezing(celsius) ? 'freezing' : (celsius > 30 ? 'hot' : 'mild');// void: the function does something but returns nothing.void report(String city, double celsius) {  String f = toFahrenheit(celsius).toStringAsFixed(1);  print('${city.padRight(8)}$celsius C = $f F, ${describe(celsius)}');}void main() {  report('Oslo', -4);  report('Madrid', 21);  report('Seville', 36.5);  // The result can be kept in a variable or used directly.  double boiling = toFahrenheit(100);  print('Water boils at $boiling F');  print('Freezing at 0 C? ${isFreezing(0)}');  // Functions are values too: they can go in a variable.  var convert = toFahrenheit;  var square = (int n) => n * n;  print('Body: ${convert(37).toStringAsFixed(1)} F, 12 squared: ${square(12)}');}

Output · dart run main.dart

Oslo    -4.0 C = 24.8 F, freezing
Madrid  21.0 C = 69.8 F, mild
Seville 36.5 C = 97.7 F, hot
Water boils at 212.0 F
Freezing at 0 C? true
Body: 98.6 F, 12 squared: 144

Key points

  • type name(parameters) { return ...; } defines a function.
  • => expression replaces { return expression; }.
  • void means it returns nothing.
  • Functions are values: they can be stored in variables.
  • (x) => ... is an anonymous function.

Exercise

Write a kmToMiles function with arrow syntax and a void function called trip that prints a destination with its distance in kilometres and in miles; call it with three destinations.