Basic usage of functions

  • A basic function consists of passing arguments, implementing and returning values. The return value may not be written, but even if the function takes no arguments and does nothing, the format is still written. Here is a simple function
main(List<String> args) { int a = sum(1, 10); print(a); final b = sum1(2, 3); sum2; } int sum(int num1,int num2){return num1+num2; } sum1(int num1,int num2){ return num1+num2; } // You can pass in no values and implement nothing internally, but the format must be sum2(){}Copy the code

Parameters of a function

The Dart function has mandatory and optional parameters. Optional parameters can have default values, but mandatory parameters do not have default values. Mandatory parameters require that values be passed in when the Dart function is called

Will choose parameters

  • Mandatory parameters of the Dart function are the parameters that must be passed when the Dart function is called. If the Dart function is not passed, an error will be reported

Optional parameters

As the name implies, optional parameters of the Dart function can be selected, which can be transmitted or not transmitted. At the same time, the writing method of optional parameters can be divided into

The location and naming parameters are optional

Location This parameter is optional

  • Positional optional arguments, as their name implies, match optional arguments in a fixed position.
  • The optional position parameters are named in the format of [id obj]. When passing parameters, pass the values corresponding to the declaration type in the corresponding sequence. If you do not need to pass them, do not pass them
Void sayHellow2(String name, [int age, double height]){print("name $name age $age height $height"); } sayHellow2 (" lilei, "18,18.8); SayHellow2 ("lilei2"); sayHellow2("lilei2"); SayHellow2 ("lilei",18) sayHellow2("lilei",18Copy the code
  • Positional arguments can have default values and can be assigned at declaration time
Void sayHellow2(String name, [int age = 1, double height = 2.0]){print("name $name age $age height $height"); } Dart: Dart: Dart: Dart: Dart: Dart: Dart: Dart: Dart: Dart: Dart: Dart: Dart: Dart: Dart: Dart: Dart: Dart: Dart: Dart: Dart: Dart: Dart: Dart: Dart: Dart: Dart: Dart: Dart: Dart: Dart: Dart: Dart: Dart: Dart: Dart: Dart: Dart: Dart: Dart: DartCopy the code
SayHellow2 (” LILei “,null,18.8) sayHellow2(” LILei “,null,18.8) sayHellow2(“lilei”,null,18.8) sayHellow2 Is there a way I can pass no value but still have a default value? Dart provides an alternative way to address this requirement: name optional parameters

This parameter is optional

  • Named parameters match optional parameters in key-value pairs
  • Named parameters are named in the form of {key,key2}, separated by commas. When passed in, they match the optional parameters of the declaration type in the form of key: value
SayHellow3 (String name, {int age, double height}){} sayHellow3("lilei", height:18.8,age: 18); // Name optional parameters. The system will match the parameter name, because the name is referenced, so the order is not requiredCopy the code
  • Named parameters can have default values and can be assigned at declaration time
Because of the flexibility of named parameters, we can skip optional parameters we don’t want passed in, so named parameters are the optional parameters we use most in our daily projects