Dart series: functions in dart language

Posted by planetsim on Wed, 10 Nov 2021 13:28:00 +0100

catalogue

brief introduction

Function is the content of all programming languages. Whether object-oriented or process-oriented, function is a very important part. What is the difference between functions in dart and functions in java?

As an object-oriented programming language, dart's Function is also an object, which is represented by Function. Let's first look at the definition of the Function:

abstract class Function {
  
  external static apply(Function function, List<dynamic>? positionalArguments,
      [Map<Symbol, dynamic>? namedArguments]);

  int get hashCode;

  bool operator ==(Object other);
}

Since the function is an object, you can assign the function to the object and pass the function as a parameter to other functions.

The following is a simple function, represented by return value, function name, parameter and function body:

bool isStudent(int age){
    return age < 20;
    }

Although dart recommends that we specify the return value type of the function, you can also ignore the return value:

isStudent(int age){
    return age < 20;
    }

There is another shorthand for function. If the function body has only one statement, you can use = > instead of parentheses:

isStudent(int age) => age < 20;

It looks more concise.

Parameters of function

The function parameters in dart include ordinary parameters and named parameters.

Common parameters are easy to understand, so what are named parameters?

Named parameter is to give a name to the parameter when passing, so that the function can specify the name of the parameter to assign value when calling.

Take an example of function parameters and named parameters:

bool calculator(int age , {int? size}){

}

You can call:

calculator(15,size:10);

By default, named parameters are optional, that is, the function can choose whether to pass optional parameters when calling.

If you have to pass a parameter, you can set it to required

In addition to the named parameters, dart also has optional position parameters, that is, put the parameters in [], as shown below:

String say(String from, String msg, [String? device]) {
  var result = '$from says $msg';
  if (device != null) {
    result = '$result with a $device';
  }
  return result;
}

When calling the above function, you can only pass in ordinary parameters or additional optional location parameters, as follows:

say('Bob', 'Howdy');
say('Bob', 'Howdy', 'smoke signal');

The parameters in dart can also set default values as follows:

String say(String from='mack', String msg, [String? device='ios']) {
  ...
}

main function

The main function in dart is the startup entry of the application. main() is a special function. It is a function with optional List parameters without return value, as shown below:

void main() {
  print('Hello, World!');
}
void main(List<String> arguments) {
  print(arguments);
}

Anonymous function

Most functions are named to facilitate function calls. In some cases, functions can also be unnamed. Such functions are called anonymous functions.

An anonymous function is a function without a name, as shown below:

([[Type] param1[, ...]]) {
  codeBlock;
};

Anonymous functions are usually used when they do not need to be called by other scenarios, such as traversing a list:

const list = ['apples', 'bananas', 'oranges'];
list.forEach((item) {
  print('${list.indexOf(item)}: $item');
});

closure

When you mention closures, you will immediately think of javascript. Yes, in addition to javascript, closures can also be built in dart.

What is a closure? Simply put, it's a function of a function. That is, the variables defined in a function are used by other functions outside the scope of the function.

Function sum(int age1){
    return (int i) => age1 +i; 
}

void main() {
  var sum2 = sum(2);
  var result = sum2(5);
}

In the above example, the variable 2 passed in by sum is used in the subsequent sum2.

Return value of function

All functions have a return value. If the return is not displayed, the return is null

So for the following functions:

foo() {}

Its value is null, that is, the following expression is true:

assert(foo() == null);

summary

The above is the definition of the function in Dart.

This article has been included in http://www.flydean.com/03-dart-function/

The most popular interpretation, the most profound dry goods, the most concise tutorial, and many tips you don't know are waiting for you to find!

Welcome to my official account: "those things in procedure", understand technology, know you better!

Topics: Flutter dart