VOOZH about

URL: https://www.geeksforgeeks.org/dart/different-types-of-functions-in-dart/

⇱ Different Types of Functions in Dart - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Different Types of Functions in Dart

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. 


Output:

This is the best website for 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 and a return type

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


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. 


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. 


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:

  • Use No Arguments, No Return Type when you just need to execute a task without returning a value.
  • Use No Arguments, With Return Type when you need to return a value but don't require input.
  • Use With Arguments, No Return Type when you want to process input without returning a result.
  • Use With Arguments, With Return Type when processing input and returning a computed value.

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

Comment
Article Tags:
Article Tags:

Explore