Dart: Functions

 

Functions

Functions let you package multiple related lines of code into a single body that you can summon to avoid repeating those lines of code throughout your Dart application:

Functions

A function consists of a return type, a function name, a parameter list in parentheses, and then a function body enclosed in braces. The code you're turning into the function goes inside the braces. When you call the function, you pass in arguments that match the types of the parameters of the function.

Write a new function in DartPad that will just check to see if a given string is banana.

bool isBanana(String fruit) {
  return fruit == 'banana';
}

The function uses return to return a bool value determined by the argument passed to the function. This function will always return the same value for any given input. If a function does not need to return a value, you can set the return type to void, such as for the main function.

 

Optional Parameters

If a parameter to a function is optional, you can surround it with square brackets:

String fullName(String first, String last, [String title]) {
  return "${title == null ? "" : "$title "}$first $last";
}

If an optional parameter is not included in the function call, the value used is null for the parameter inside the function body.

 

Optional Named Arguments

With Dart functions, you can use optional named arguments by surrounding the parameter list with braces:

bool withinTolerance({int value, int min, int max}) {
  return min <= value && value <= max;
}

Default Values

You can also assign default values to one or more parameters using equals:

bool withinTolerance({int value, int min = 0, int max = 10}) {
  return min <= value && value <= max;

First-Class Functions

Dart has support for what are called first-class functions. The term means that functions are treated like any other data type, that is, they can be assigned to variables, passed as arguments, and returned from other functions.

You can use the Function type to specify that a parameter named op is a function itself:

int applyTo(int value, int Function(int) op) {
  return op(value);
}

So if you have a function named square:

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

Arrow Syntax

When a Dart function, either named or anonymous, consists of just one line of code, you can reduce the function body using the Dart arrow syntax.

For example, multiply above becomes:

var multiply = (int a, int b) => a * b;

You've removed the braces and the return statement.

You can also use arrow syntax with applyMultiplier:

Function applyMultiplier(num multiplier) =>
  (num value) => value * multiplier;

You've used arrow syntax twice here: the first is denoting the return value for the applyMultiplier function, the second is within the return value itself, which is an anonymous function.

In the example just above using forEach, you cannot use arrow syntax, since the body of the function you're passing to forEach has more than one line of code.

 

Comments

Popular posts from this blog

Flutter: Exploring more widgets

Food Delivery App 5 - Firebase Storage for Profile Picture

Flutter: Everything is a widget