03. Flutter develops Dart-functions

1. Function

1.1. Basic definition of function

Dart is a true object-oriented language, so even functions are objects, all have types, and the type is Function.
This means that functions can be defined as variables or as parameters or return values ​​of other functions.
Functions are defined in the following way:

返回值 函数的名称(参数列表) {
    
    
  函数体
  return 返回值
}

According to the definition above, we define a complete function:

int sum(num num1, num num2) {
    
    
  return num1 + num2;
}

Effective Dart recommends using type annotations for public APIs, but if we omit the type, it will still work

sum(num1, num2) {
    
    
  return num1 + num2;
}

Also, if there is only one expression in the function, you can use arrow syntax

  • Note that there can only be one expression, not a statement
sum(num1, num2) => num1 + num2;

1.2. Function parameter problem

Function parameters can be divided into two categories: required parameters and optional parameters.
The parameters used above are all required parameters.

1.2.1. Optional parameters

Optional parameters can be divided into named optional parameters and positional optional parameters
Definition method:

命名可选参数: {
    
    param1, param2, ...} 
位置可选参数: [param1, param2, ...]

Demonstration of named optional parameters:


// 命名可选参数
printInfo1(String name, {
    
    int age, double height}) {
    
    
  print('name=$name age=$age height=$height');
}

// 调用printInfo1函数
printInfo1('why'); // name=why age=null height=null
printInfo1('why', age: 18); // name=why age=18 height=null
printInfo1('why', age: 18, height: 1.88); // name=why age=18 height=1.88
printInfo1('why', height: 1.88); // name=why age=null height=1.88

Demonstration of positional optional parameters:


// 定义位置可选参数
printInfo2(String name, [int age, double height]) {
    
    
  print('name=$name age=$age height=$height');
}

// 调用printInfo2函数
printInfo2('why'); // name=why age=null height=null
printInfo2('why', 18); // name=why age=18 height=null
printInfo2('why', 18, 1.88); // name=why age=18 height=1.88

Name optional parameters, you can specify that a parameter is mandatory (using @required, there is a problem)

// 命名可选参数的必须
printInfo3(String name, {
    
    int age, double height, @required String address}) {
    
    
  print('name=$name age=$age height=$height address=$address');
}

1.2.2. Parameter default values

Parameters can have default values, if not passed in, use the default value

  • Note that only optional parameters can have default values, and mandatory parameters cannot have default values
// 参数的默认值
printInfo4(String name, {
    
    int age = 18, double height=1.88}) {
    
    
  print('name=$name age=$age height=$height');
}

The main function in Dart is one that accepts optional list parameters as parameters, so when using the main function, we can pass in parameters or not

1.3. Functions are first-class citizens

In many languages, functions cannot be used as first-class citizens, such as Java/OC. This limitation makes programming not flexible enough, so modern programming languages ​​basically support functions as first-class citizens, and Dart also supports them. This
is It means that you can assign a function to a variable, or use a function as a parameter or return value of another function.


main(List<String> args) {
    
    
  // 1.将函数赋值给一个变量
  var bar = foo;
  print(bar);

  // 2.将函数作为另一个函数的参数
  test(foo);

  // 3.将函数作为另一个函数的返回值
  var func =getFunc();
  func('kobe');
}

// 1.定义一个函数
foo(String name) {
    
    
  print('传入的name:$name');
}

// 2.将函数作为另外一个函数的参数
test(Function func) {
    
    
  func('coderwhy');
}

// 3.将函数作为另一个函数的返回值
getFunc() {
    
    
  return foo;
}

1.4. Use of anonymous functions

Most of the functions we define will have their own names, such as the foo and test functions defined earlier.
But in some cases, it is too troublesome to name the function. We can use a function without a name. This function can be called an anonymous function (anonymous function), or it can be called a lambda or a closure.

main(List<String> args) {
    
    
  // 1.定义数组
  var movies = ['盗梦空间', '星际穿越', '少年派', '大话西游'];

  // 2.使用forEach遍历: 有名字的函数
  printElement(item) {
    
    
    print(item);
  }
  movies.forEach(printElement);

  // 3.使用forEach遍历: 匿名函数
  movies.forEach((item) {
    
    
    print(item);
  });
  movies.forEach((item) => print(item));
}

1.5. Lexical scoping

The lexical in dart has its own clear scope. It determines the scope according to the structure of the code ({}). The
variable in its own scope is used first. If it is not found, it will search outward layer by layer.

var name = 'global';
main(List<String> args) {
    
    
  // var name = 'main';
  void foo() {
    
    
    // var name = 'foo';
    print(name);
  }

  foo();
}

1.6. Lexical Closures

Closures can access variables within their lexical scope, even if the function is used elsewhere, they can be accessed normally.

main(List<String> args) {
    
    
  makeAdder(num addBy) {
    
    
    return (num i) {
    
    
      return i + addBy;
    };
  }

  var adder2 = makeAdder(2);
  print(adder2(10)); // 12
  print(adder2(6)); // 8

  var adder5 = makeAdder(5);
  print(adder5(10)); // 15
  print(adder5(6)); // 11
}

1.7. Return value problem

All functions return a value. If no return value is specified, the statement returns null; implicitly attached to the function body.

main(List<String> args) {
    
    
  print(foo()); // null
}

foo() {
    
    
  print('foo function');
}

Guess you like

Origin blog.csdn.net/qq_25218777/article/details/116401986