Different Types of Functions in Dart

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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.