Summary: in this tutorial, you’ll understand what Dart functions are first-class citizens means.
In Dart, functions are first-class citizens. This means that you can treat a function as a value of other types. So you can:
- Assign a function to a variable.
- Pass a function to another function as an argument.
- Return a function from a function.
Assigning a function to a variable
The following example shows how to assign a function to a variable:
int add(int x, int y) {
return x + y;
}
void main() {
var fn = add;
var result = fn(10, 20);
print(result);
}
Code language: Dart (dart)
Output:
30
Code language: Dart (dart)
How it works.
First, define the add()
function that accepts two integers and returns the sum of them:
int add(int x, int y) {
return x + y;
}
Code language: Dart (dart)
Second, assign the add
function name to the fn
variable:
var fn = add;
Code language: Dart (dart)
Note that you don’t use the parentheses ()
after the add
function name.
Third, call the add
function via the fn
variable and assign the return value to the result
variable:
var result = fn(10, 20);
Code language: Dart (dart)
Finally, display the result:
print(result);
Code language: Dart (dart)
Passing a function to another function as an argument
The following program illustrates how to pass a function to another function as an argument:
bool isOddNumber(int x) {
return x % 2 != 0;
}
bool isEvenNumber(int x) {
return x % 2 == 0;
}
void show(Function fn) {
for (int i = 0; i < 10; i++) {
if (fn(i)) {
print(i);
}
}
}
void main() {
print("Even numbers:");
show(isEvenNumber);
print("Odd numbers:");
show(isOddNumber);
}
Code language: Dart (dart)
Output:
Even numbers:
0
2
4
6
8
Odd numbers:
1
3
5
7
9
Code language: Dart (dart)
How it works.
First, define isOddNumber()
function that returns true
if a number is odd:
bool isOddNumber(int x) {
return x % 2 != 0;
}
Code language: Dart (dart)
Next, define isEvenNumber()
function that returns true
if a number is even:
bool isEvenNumber(int x) {
return x % 2 == 0;
}
Code language: Dart (dart)
Then, define the show()
function that accepts a function as an argument. Note that all functions have the type Function
:
void show(Function fn) {
for (int i = 0; i < 10; i++) {
if (fn(i)) {
print(i);
}
}
}
Code language: Dart (dart)
Inside the show()
function, iterate from 0 to 10. In each iteration, display the value if it makes the result of the fn()
true.
After that, call the show
function and pass the isEvenNumber
as an argument:
print("Even numbers:");
show(isEvenNumber);
Code language: Dart (dart)
This function call will display all the even numbers from 0 to 9.
Finally, call the show()
function and pass the isOddNumber
as an argument:
print("Odd numbers:");
show(isOddNumber);
Code language: Dart (dart)
This function call will display all odd numbers from 0 to 9.
Returning a function from a function
The following example illustrates how to return a function from a function:
add(int x, int y) {
return x + y;
}
subtract(int x, int y) {
return x - y;
}
Function calculation(String op) {
if (op == '+') return add;
if (op == '-') return subtract;
return add;
}
void main() {
var fn = calculation('+');
print(fn(10, 20));
fn = calculation('-');
print(fn(30,20));
}
Code language: Dart (dart)
Output:
30
10
Code language: Dart (dart)
How it works.
First, define the add()
function that returns the sum of two integers:
add(int x, int y) {
return x + y;
}
Code language: Dart (dart)
Next, define the subtract function that returns the subtraction of two integers:
subtract(int x, int y) {
return x - y;
}
Code language: Dart (dart)
Then, define calculation()
function that returns the add function if the argument is '+'
or the subtract function if the argument is '-'
:
Function calculation(String op) {
if (op == '+') return add;
if (op == '-') return subtract;
return add;
}
Code language: Dart (dart)
After that, call the calculation function, and assign the result to the fn
variable, and call the function via the fn
variable:
var fn = calculation('+');
print(fn(10, 20));
Code language: Dart (dart)
This code displays 30
because the calculation()
function returns the add
function.
Finally, call the calculation()
function, assign the result to the fn
variable, and call the function via the fn
variable:
fn = calculation('-');
print(fn(30, 20));
Code language: Dart (dart)
This code display 10
because the calculation()
function returns the subtract
function.
Summary
- Dart functions are first-class citizens means that you can assign a function to a variable, pass a function to another function as an argument, and return a function from another function.