DEV Community

Cover image for Types of function in Dart
Jay Tillu😎
Jay Tillu😎

Posted on • Updated on

Types of function in Dart

Let’s discuss the types of function. Remember that function can be categorized in any manner. But here we categorized them based on arguments and return type.

first of all, remember that one function can only return one type. And both return_type and return_value must be the same. Like if your function is String type then it must return a string value. It cannot return an int or double type.

There are four main types of user define functions (based on arguments and return type).

  1. Function with no arguments and no return type
  2. Function with arguments and no return type
  3. Function with no arguments and return type
  4. Function with arguments and with return type

Function with no arguments and no return type


Syntax

void function_name(){
  // Statements
}
Enter fullscreen mode Exit fullscreen mode

Sample Program

void SayMyName(){
  print("Jay Tillu");    
}
main(){
  SayMyName();
}

Output
Jay Tillu
Enter fullscreen mode Exit fullscreen mode

Function with no arguments and return type


In this category, the function has no arguments but rather it has a return type.

Syntax

return_type function_name() {
  // Statements
  return value;
}
Enter fullscreen mode Exit fullscreen mode

Sample Program

int ShowMyAge(){
  int age = 20;
  return age;
}
main(){
  int myAge = ShowMyAge();
  print(myAge);
}

Output
20
Enter fullscreen mode Exit fullscreen mode

Function with arguments and no return type


  • Here our function has arguments but it does not have any return type.

  • When there are arguments in a function, we have to specify each argument’s value when we call the function. Otherwise, it will give you a runtime error. So be careful about that.

Syntax

function_name(args1, args2, ...argsN){
  // Statements
}
Enter fullscreen mode Exit fullscreen mode

Sample Program

AboutMySelf(int age, int totalGf) {
  print(age);
  print(totalGf);
}

main() {
  AboutMySelf(20, 0);
}

Output
200
Enter fullscreen mode Exit fullscreen mode

Function with arguments and with return type


Congrats!! now our function has arguments and return type both. 😎

Syntax

return_type function_name(args1, args2, ...argsN) {
  //statements
  return value;
}
Enter fullscreen mode Exit fullscreen mode

Sample Program

int Sum(int numberOne, int numberTwo) {
  int addition = numberOne + numberTwo;
  return addition;
}

main() {
  int mySum = Sum(20, 30);
  print(mySum);
}

Output
Sum is 50
Enter fullscreen mode Exit fullscreen mode

Here if you don’t specify any return type, by default it’s considered as void. But it’s good practice if you specify the void if there is no return type.

Remember no teacher, no book, no video tutorial, or no blog can teach you everything. As one said Learning is Journey and Journey never ends. Just collect some data from here and there, read it, learn it, practice it, and try to apply it. Don’t feel hesitate that you can’t do that or you don’t know this concept or that concept. Remember every programmer was passed from the path on which you are walking right now. Remember Every Master was Once a Beginner. Work hard and Give your best.

For More Information Please Visit Following Links

Jai Hind, Vande Mataram 🇮🇳

Wanna get in touch with me? Here are links. I’ll love to become your friend. 😊

Top comments (0)