Skip to content
Related Articles

Related Articles

Different Types of Functions in Dart
  • Last Updated : 21 Jul, 2020

The function is a set of statements that take inputs, do some specific computation, and produces output. Functions are created when certain statements are repeatedly occurring in the program and a function is created to replace them. Functions make it easy to divide the complex program into smaller sub-groups and increase the code reusability of the program. 

Basically there are four types of functions in Dart. These are as follows:

  • No arguments and no return type
  • With arguments and no return type
  • No arguments and return type
  • With arguments and with return type

1. Function with no argument and no return type:

Basically in this function, we do not give any argument and expect no return type. It can be better understood by an example. 
 

Dart




void myName(){
  print("GeeksForGeeks");
}
  
void main(){
  print("This is the best website for developers:");
  myName();
}

So myName is the function which is void means it is not returning and anything and the empty pair of parentheses suggest that there is no argument which is passed to the function.



2. Function with arguments and no return type:

Basically in this function, we are giving an argument and expect no return type. 

Dart




int myPrice(){
  int price = 0;
  return price;
}
  
void main(){
  int Price = myPrice();
  print("GeeksforGeeks is the best website for developers which costs : ${Price}/-");
}

So myPrice is the function which is int means it is returning int type and the empty pair of parentheses suggests that there is no argument which is passed to the function.

3. Function with no arguments and return type:

Basically in this function, we do not give any argument but expect a return type. 
 

Dart




myPrice(int price){
  print(price);
}
void main(){  
  print("GeeksforGeeks is the best website for developers which costs : ");
  myPrice(0);
}

So myPrice is the function which is void means it is not returning anything and the pair of parentheses is not empty this time that suggests that it accept an argument. 

4. Function with arguments and with return type:

Basically in this function, we are giving an argument and expect return type. It can we better understand by an example. 
 

Dart




int mySum(int firstNumber, int secondNumber){
  return (firstNumber + secondNumber);
}
void main(){
  int additionOfTwoNumber = mySum(100, 500);
  print(additionOfTwoNumber);
}

So mySum is the function which is int means it is returning int type and the pair of parentheses is having two arguments which are used further in this function and then in the main function we are printing the addition of two numbers.

My Personal Notes arrow_drop_up
Recommended Articles
Page :