728x90
반응형
void main() {
Operation operation = add;
int result = operation(10, 20, 30);
print(result);
operation = subtract;
int result2 = operation(50,40,30);
print(result2);
int result3 = calculate(100 , 20 , 30 , add);
print(result3);
}
//signature
typedef Operation = int Function(int x , int y , int z);
int add(int x , int y , int z) => x + y + z;
int subtract(int x , int y , int z) => x - y - z;
int calculate(int x , int y , int z , Operation operation){
return operation(x , y, z);
}
## typedef를 이용하여 함수의 형태를 지정한다.
## 위에서 지정한 형태의 함수와 동일한 형태의 add , subtract 함수를 생성한다.
## signature인 Operation을 선언한후 사용할 함수를 넣어준다.
## calculate 함수는 인자값으로 적용할 함수를 받아 리턴해준다
728x90
반응형
'개발일기 > Dart' 카테고리의 다른 글
Dart #6 Async (0) | 2024.02.14 |
---|---|
Dart #5 Functional Programming (1) | 2024.02.08 |
Dart #4 OOP (0) | 2024.02.02 |
Dart #2 DATA TYPES (0) | 2023.11.29 |
Dart #1 VARIABLES (0) | 2023.11.29 |