Introduction

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

As an object-oriented programming language, dart's function is also an object, which is represented by Function. 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 a function is an object, you can assign the function to the object, and you can also pass the function as a parameter to other functions.

The following is a simple function, represented by the return value, function name, parameters 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 also a shorthand for the function. If the function body has only one statement, then you can use => to replace the parentheses:

isStudent(int age) => age < 20;

It looks more concise.

Function parameters

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

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

The named parameter is to give a name to the parameter when it is passed, so that when the function is called, the name of the parameter can be specified to assign the value.

Look at an example of function parameters and named parameters:

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

}

It can be called like this:

calculator(15,size:10);

Named parameters are optional by default, which means that when the function is called, you can choose whether to pass optional parameters.

If you must pass a parameter, you can set it to required.

In addition to named parameters, dart also has optional positional 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 the above function is called, only ordinary parameters can be passed in, or optional positional parameters can be additionally passed in, as follows:

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

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

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

main function

The main function in dart is the start entry of the application, main() is a special function, it is a function with optional List<String> parameters without a 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 the calling of functions. In some cases, the function may not have a name. Such a function is called an anonymous function.

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

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

Anonymous functions are usually used in situations that 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 it comes to closures, everyone will immediately think of javascript. Yes, in addition to javascript, closures can also be built in dart.

What is a closure? Simply put, it is a function of a function. That is, 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.

The return value of the function

All functions have a return value, if there is no display return, then the return is null.

So for the following function:

foo() {}

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

assert(foo() == null);

Summarize

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 discover!

Welcome to pay attention to my official account: "Program those things", know technology, know you better!


flydean
890 声望433 粉丝

欢迎访问我的个人网站:www.flydean.com