Open In App

Different Types of Functions in Dart

Last Updated : 09 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The function is a set of statements that take inputs, do some specific computation, and produce 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:

  1. No arguments and no return type 
  2. With arguments and no return type 
  3. No arguments and return type 
  4. 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();
}


Output:

This is the best website for the developers : GeeksForGeeks

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

2. Function with no arguments but return type:

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

Dart




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


Output:

GeeksforGeeks is the best website for developers which costs : 0/-

So myPrice is the function that 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 arguments but no return type:

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

Dart




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


Output:

GeeksforGeeks is the best website for developers which costs : 0

So myPrice is the function that is void means it is not returning anything and the pair of parentheses is not empty this time which 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 be better understood by an example. 

Dart




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


Output:

600

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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads