Dart Types of Functions
Functions are the set of statements intended to perform a specific task. They accept some input from the user, perform the specified computation and generate the output. They are very useful as they reduce the time and memory of re-writing the same lines of code repeatedly.
Functions make it easier to divide the larger programs into smaller sub-groups known as modules, and this type of programming refers to modular programming. They also enhance the reusability and readability of the code.
An important point to remember here is that a function can only have one return type, and the return type and function's return value must be the same.
Functions can be categorized based on arguments and return type as follows :
- Function with no arguments and no return type
- Function with arguments and no return type
- Function with no arguments and return type
- Function with arguments and with return type
Let us study each one of them in detail:
Function with no arguments and no return type
In this type of function, we do not provide any argument to the function and neither do we provide any return type to it, which is basically 'void'.
Let us consider the following example to understand this type of function,
Program
/* defining the function display( )
Cleary there is no argument to this function and the return type is void which
technically means that the function doesn't return anything */
void display( )
{
print( ' This is the example of function with no return type and no arguments ! ' ) ;
// no return statement
}
void main( )
{
print( ' Let us see : ' ) ;
/* call to the function display.
Since the function originally did not have any argument or return statement, we do
not provide either of them in the call statement */
display( ) ;
}
Output:
This is the example of function with no return type and no arguments!
Function with no argument but return type
In this type of function, we do not provide any arguments to the function, but it has a return type which can be int or double.
It must be ensured that the return type and return value of the function must be same.
Let us consider the following example to understand this type of function,
Program
/* defining the function increment( ) which increments the value of a by 1
Cleary there is no argument to this function but the return type is int which
technically means that the function returns an integer value */
int increment( )
{
int a = 10 ;
print(' Value of a before incrementing : ' ) ;
print( a ) ;
a ++ ;
return a ; // return statement
}
void main( )
{
int a ;
/* call to the function increment
Since the function originally did not have any arguments, we did not pass any
in the call statement. But a variable of type int is declaared to hold the
integer value returned by the function. */
a = increment( ) ;
print( ' Let us see the value of a after incrementing : ' ) ;
print( a ) ;
}
Output
Value of a before incrementing :
10
Let us see the value of a after incrementing :
11
Functions with arguments but no return type
In this type of function, we provide arguments to the function but it has no return type which means we use void as the return type.
Let us consider the following example to understand this type of function,
Example -
/* defining the function increment() which increments the value of a by 1
Here we provide an argument to this function but the return type is void which
technically means that the function does not return anything */
void increment( int a )
{
print( ' Value of a before incrementing : ' ) ;
print( a ) ;
a ++ ;
print( ' Value of a after incrementing : ' ) ;
print( a ) ;
}
void main( )
{
int a = 5 ;
/* call to the function increment
Since the function declartion had one argument int a, we pass an integer variable to this function which passes the value 5 to that function for computation. */
increment( a ) ;
}
Output
Vale of a before incrementing :
5
Value of a after incrementing :
6
Functions with arguments and return type
In this type of function, we provide both argument and the return type of the function which can be int or double.
Let us consider the following example to understand this type of function,
Example -
/* defining the function increment() which increments the value of a by 1
Here we provide both an argument and the return type to the function.
The return type is integer which means the function returns an integer value. */
int increment( int a )
{
print( ' Value of a before incrementing : ' ) ;
print( a ) ;
a ++ ;
print( ' Value of a after incrementing : ' ) ;
return a ; // return statement
}
void main( )
{
int a = 5 ;
/* call to the function increment
Since the function declaration had one argument int a, we pass an integer
Variable to this function which passes the value 5 to that function for computation.
A variable is used to hold the value returned by the increment( ) function */
a = increment( a ) ;
print( a ) ;
}
Output:
Value of a before incrementing :
5
Value of a after incrementing :
6
There are three other types of functions as well, which are as follows :
- Dart Recursive Functions
- Dart Lambda Functions / Dart Arrow Functions
- Dart Anonymous Function