-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path29_higher_order_functions.dart
More file actions
36 lines (22 loc) · 936 Bytes
/
29_higher_order_functions.dart
File metadata and controls
36 lines (22 loc) · 936 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// Objectives
// 1. Higher-Order Function:
// How to pass function as parameter?
// How to return a function from another function?
void main() {
// Example One: Passing Function to Higher-Order Function
Function addNumbers = (a, b) => print(a + b);
someOtherFunction("Hello", addNumbers);
// Example Two: Receiving Function from Higher-Order Function
var myFunc = taskToPerform();
print(myFunc(10)); // multiplyFour(10) // number * 4 // 10 * 4 // OUTPUT: 40
}
// Example one: Accepts function as parameter
void someOtherFunction(String message, Function myFunction) { // Higher-Order Function
print(message);
myFunction(2, 4); // addNumbers(2, 4) // print(a + b); // print(2 + 4) // OUTPUT: 6
}
// Example two: Returns a function
Function taskToPerform() { // Higher-Order Function
Function multiplyFour = (int number) => number * 4;
return multiplyFour;
}