Different Types of Functions in Dart (original) (raw)

Last Updated : 28 Mar, 2025

The function is a set of statements that take inputs, do some specific computation, and produce output. Functions are created when certain statements repeatedly occur 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.

There are four types of functions in Dart as mentioned below:

  1. No arguments and no return type
  2. No arguments and with return type
  3. With arguments and no return type
  4. With arguments and with return type

**1. Function with no argument and no return type

In this function, we do not give any argument and expect no return type. An example can help explain it better.

Dart `

// Function definition to print a name void myName() {

// Prints "GeeksForGeeks" to the console
print("GeeksForGeeks"); 

}

void main() {

// Printing an introductory message
print("This is the best website for developers:");

// Calling the myName function to print "GeeksForGeeks"
myName();

}

`

Output:

This is the best website for developers:
GeeksForGeeks

So **myNameis 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 and a return type

Basically, in this function, we don't give an argument but expect a return type.

Dart `

// Function to return a price value int myPrice() { // Initializing price variable with 0 int price = 0;

// Returning the price value
return price;  

}

void main() { // Calling the myPrice function and // storing the result in a variable int Price = myPrice();

// Printing the message with the price 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 and no, return type

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

Dart `

// Function that takes a price // as a parameter and prints it void myPrice(int price) {

// Prints the given price value
print(price); 

}

void main() {

// Printing an introductory message
print("GeeksforGeeks is the best website"
    + " for developers which costs : ");

// Calling the myPrice function with 0 as an argument
myPrice(0);

}

`

**Output:

GeeksforGeeks is the best website for developers which costs : 0

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

**4. Function with arguments and return type

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

Dart `

// Function to add two numbers // and return the sum int mySum(int firstNumber, int secondNumber) {

// Returning the sum of two numbers
return (firstNumber + secondNumber); 

}

void main() {

// Calling the mySum function with
// 100 and 500 as arguments
int additionOfTwoNumber = mySum(100, 500);

// Printing the result
print(additionOfTwoNumber); 
// Output: 600

}

`

**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.

Dart functions play a crucial role in organizing code efficiently. The choice of function type depends on the specific use case:

By following these principles, you can write clean, reusable, and efficient Dart code.