1. Definition of function
Return value The name of the function (argument list) {function bodyreturnThe return value}Copy the code
Define a simple calculation to add two numbers in the format above:
num sum(num a, num b) {
return a + b;
}
Copy the code
The argument and return value types of the function can also be omitted, so that the function can evaluate all the processing that supports + and automatically derive the corresponding type:
print(sum("abc"."def").runtimeType); // String
print(sum(1.2).runtimeType); // int
print(sum(1.2.0).runtimeType); // double
print(sum(1.0.2.0).runtimeType); // double
print(sum([1.2.3], [4.5.6]).runtimeType); // List<int>
sum(a, b) {
return a + b;
}
Copy the code
The sum function above can be written as:
sum(a, b) => a + b;
Copy the code
All functions return a value. If no value is returned, null is returned by default.
2. This parameter is optional
To implement optional parameters, you can use two methods:
Name Optional parameters: {param1, param2... } Position Optional parameters: [param1, param2,...]Copy the code
1. Name optional parameter
printInfo1("Jack", height: 1.88, age: 18);
void printInfo1(String name, {int age, double height}) {
print("name: $name, age: $age, height: $height");
}
Copy the code
Name optional parameters The optional parameters are expanded with {} and defined after the required parameters. When the function is called, the required parameters are passed in first and then in the format of alias: parameter value. Because parameter aliases are passed, naming optional parameters does not need to be passed in the order in which the function is declared. The components of a Flutter pass values with this naming optional argument, for example:
Widget build(BuildContext context) {
return MaterialApp(
home: HomePage(),
debugShowCheckedModeBanner: false,); } Widget build(BuildContext context) {return MaterialApp(
debugShowCheckedModeBanner: false,
home: HomePage(),
);
}
Copy the code
MaterialApp initialization method, in the home and debugShowCheckedModeBanner parameters of incoming order can change, you can see by looking at the source code, all parameters are use {} expansion, name optional parameters are defined as:
const MaterialApp({
Key key,
this.navigatorKey,
this.home,
this.routes = const <String, WidgetBuilder>{},
this.initialRoute,
...
})
Copy the code
In the Flutter development environment, you can also add the @required keyword to optional naming parameters to make them mandatory:
void printInfo1(
String name,
{
@required int age,
double height,
}) {
print("name: $name, age: $age, height: $height");
}
Copy the code
This is done because in DART, mandatory parameters are not aliased by default and must be passed in the order they are declared, whereas optional parameters can be aliased, sequential and mandatory by naming them with the @required keyword. Declarations of this argument are used extensively in Flutter, such as the onPressed argument in RaisedButton initialization method:
const RaisedButton({
Key key,
@required VoidCallback onPressed,
VoidCallback onLongPress,
ValueChanged<bool> onHighlightChanged,
ButtonTextTheme textTheme,
...
})
Copy the code
2. This parameter is optional
Position optional arguments When passing a parameter, there is no alias for the parameter and it must be passed at the corresponding position:
void printInfo2(String name, [int age, double height]) {
print('name=$name age=$age height=$height');
}
Copy the code
3. Default values
Default values can be set only with optional parameters:
void printInfo1(String name, {int age = 0.double height = 1.7{})print('name=$name age=$age height=$height');
}
Copy the code
3. The function is a first-class citizen
In iOS development, Objective-C differs from Swift in that it does not allow functions to be used as first-class citizens. Functions cannot be used as variables and parameters, and return values. Dart supports this feature.
1, the function is assigned to the variable
var sunFunc = sum;
print(sunFunc(1.2));
sum(a, b) => a + b;
Copy the code
2. Function as argument
Create a function that takes two int values, and a function that takes two int arguments and returns one int. In the function body, call the function passed in, pass in the first two arguments, and return the final values:
int test(int a, int b, int f(int e1, int e2)) {
return f(a, b);
}
Copy the code
We can pass the function as an argument, and when the function is an argument, we just write the function name:
print(test(3.4, sum)); / / 7
print(test(5.3, sub)); / / 2
int sum(int a, int b) => a + b;
int sub(int a, int b) => a - b;
int test(int a, int b, int f(int e1, int e2)) {
return f(a, b);
}
Copy the code
3. Anonymous functions
Again, the function defined above:
int test(int a, int b, int f(int e1, int e2)) {
return f(a, b);
}
Copy the code
This is done by passing in the name of a defined function. The following is done by passing in the name of an anonymous function:
print(test(2.3, (a, b){
return a * b;
})); / / 6
Copy the code
If the function to be passed has only one expression, we can use the arrow expression shorthand:
print(test(2.3, (a, b) => a * b)); / / 6
Copy the code
Anonymous functions are useful in Flutter development in many places, such as the List traversal:
List<int> list = [1.2.3];
list.forEach((element) => print(element));
Copy the code
Or in Flutter development, RaisedButton onPressed:
RaisedButton(
child: Text("+", style: TextStyle(fontSize: 20, color: Colors.white),),
color: Colors.pink,
onPressed: (){
print("+ pressed"); },),Copy the code
4. Lexical closures
Closures can access variables within their lexical scope, even if the function is used elsewhere.
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
}
Copy the code