Functions in Dart
Functions are a set of statements that are grouped together to perform a specific task. Functions are also known as methods, sub-routines, or procedures. Functions are used to achieve code reusability. Dart supports the following types of functions:
- Named Functions
- Anonymous Functions
- Lambda Functions
Named Functions
A named function is a set of statements that performs a specific task. A named function can be declared anywhere in the program. A named function can be called multiple times in a program.
Syntax
return_type function_name (parameter_list) {
//statements
}
Example
void main() {
print("The sum of 2 and 3 is ${sum(2, 3)}");
}
int sum(int num1, int num2) {
return num1 + num2;
}
Anonymous Functions
An anonymous function is a function that is not declared with the function name. An anonymous function can be assigned to a variable. An anonymous function can be passed as an argument to another function. An anonymous function can be returned from another function.
Syntax
(parameter_list) {
//statements
}
Example
void main() {
var sum = (int num1, int num2) {
return num1 + num2;
};
print("The sum of 2 and 3 is ${sum(2, 3)}");
}
Lambda Functions
A lambda function is a function that is not declared with the function name. A lambda function can be assigned to a variable. A lambda function can be passed as an argument to another function. A lambda function can be returned from another function. A lambda function can be used as an anonymous function.
Syntax
(parameter_list) => expression
Example
void main() {
var sum = (int num1, int num2) => num1 + num2;
print("The sum of 2 and 3 is ${sum(2, 3)}");
}
Arugments and Parameters in Functions
Arguments are the values that are passed to the function when the function is called. Parameters are the variables that are used to receive the values when the function is called. Dart supports the following types of parameters:
- Required Parameters
- Optional Parameters
- Named Parameters
Required Parameters
Dart supports required parameters. Required parameters are enclosed in parentheses (). Required parameters cannot have default values. Required parameters cannot be null.
Syntax
return_type function_name (parameter_list) {
//statements
}
Example
void main() {
print("The sum of 2 and 3 is ${sum(2, 3)}");
print("The sum of 2, 3 and 4 is ${sum(2, 3, 4)}");
}
int sum(int num1, int num2, int num3) {
return num1 + num2 + num3;
}
Optional Parameters
Dart supports optional parameters. Optional parameters can be named or positional. Optional parameters are enclosed in square brackets []. Optional parameters can have default values. Optional parameters can be null.
Syntax
return_type function_name (parameter_list) {
//statements
}
Example
void main() {
print("The sum of 2 and 3 is ${sum(2, 3)}");
print("The sum of 2, 3 and 4 is ${sum(2, 3, 4)}");
}
int sum(int num1, int num2, [int num3]) {
int sum = num1 + num2;
if (num3 != null) {
sum += num3;
}
return sum;
}
Named Parameters
Dart supports named parameters. Named parameters are enclosed in curly braces . Named parameters can have default values. Named parameters can be null.
Syntax
return_type function_name (parameter_list) {
//statements
}
Example
void main() {
print("The sum of 2 and 3 is ${sum(num2: 2, num1: 3)}");
print("The sum of 2, 3 and 4 is ${sum(num3: 2, num1: 3, num2: 4)}");
}
int sum({int num1, int num2, int num3 = 0}) {
int sum = num1 + num2;
if (num3 != null) {
sum += num3;
}
return sum;
}
Optional Parameters with Named Parameters
Dart supports optional parameters with named parameters. Optional parameters with named parameters are enclosed in square brackets []. Optional parameters with named parameters can have default values. Optional parameters with named parameters can be null.
Syntax
return_type function_name (parameter_list) {
//statements
}
Example
void main() {
print("The sum of 2 and 3 is ${sum(2, num2: 3)}");
print("The sum of 2, 3 and 4 is ${sum(2, num2: 3, num3: 4)}");
}
int sum(int num1, {int num2, int num3 = 0}) {
int sum = num1 + num2;
if (num3 != null) {
sum += num3;
}
return sum;
}
Optional Parameters with Default Values
Dart supports optional parameters with default values. Optional parameters with default values are enclosed in square brackets []. Optional parameters with default values can be null.
Syntax
return_type function_name (parameter_list) {
//statements
}
Example
void main() {
print("The sum of 2 and 3 is ${sum(2, 3)}");
print("The sum of 2, 3 and 4 is ${sum(2, 3, 4)}");
}
int sum(int num1, int num2, [int num3 = 0]) {
int sum = num1 + num2;
if (num3 != null) {
sum += num3;
}
return sum;
}
Optional Parameters with Named Parameters and Default Values
Dart supports optional parameters with named parameters and default values. Optional parameters with named parameters and default values are enclosed in square brackets []. Optional parameters with named parameters and default values can be null.
Syntax
return_type function_name (parameter_list) {
//statements
}
Example
void main() {
print("The sum of 2 and 3 is ${sum(2, num2: 3)}");
print("The sum of 2, 3 and 4 is ${sum(2, num2: 3, num3: 4)}");
}
int sum(int num1, {int num2, int num3 = 0}) {
int sum = num1 + num2;
if (num3 != null) {
sum += num3;
}
return sum;
}