Types of Functions in Dart

Types Of Function

  • Parameter And Return Type
  • Parameter And No Return Type
  • No Parameter And Return Type
  • No Parameter And No Return Type

Function With Parameter And Return Type

Here, you give the parameters and expect the return type.

Example

void main() {
  print(Multiply(2,2));
  print(Multiply(4,2));
}


int Multiply(int num1, int num2) {
  var result = num1 * num2;
  return result;
}

In this program, Multiply is the function which means int is returning int type and the pair of paranthesis is having two parameters, num1 and num2 which are used further in this function and then in the main function it prints the multiplication of two numbers.

Function With Parameter And No Return Type

Here, you don’t give the parameter but expect a return type.

Example

void main() {
  printName("John");
}

void printName(String name) {
  print("Welcome to the Thulo Technology ${name}.");
}
Show Output

In this program, printName is the function which is void means it is not returning anything and the pair of parentheses is not empty but this time that suggests it to accept an parameter.

Info

Note: void is used for no return type as it is a nonvalue-returning function.

Function With No Parameter And Return Type

Here, you give the parameter and does not expect the return type.

Example

void main() {
  print(name());
}
String name() {
  return "Ram";
}

In this program, name is the function which is String means it is returning String type and the empty pair of parentheses suggest that there is no parameter which is passed to the function.

Function With No Parameter And No Return Type

Here, you do not pass any parameter and expect no return type.

Example

void main() {
  name();
}

void name() {
  print("My name is James Shrestha");
}
Show Output

In this program, name is the function which is void means it is not returning anything and the empty pair of parantheses suggest that there is no parameter that is passed to the function.

Example

Here is the program that show four kinds of function.

// parameter and return type
  int get_add(int first_number , int second_number) {
   var total;
   total = first_number+second_number;
     return total;
}
// parameter and no return type
void  mul(int first_number , int second_number) {
   var total;
   total = first_number*second_number;
     print("total: $total");
}
// no parameter and return type
String get_greeting() {
  String greet= "Welcome";
   return greet;
}
// no parameter and no return type
 void greetings() {
     print("Good Morning, Sushil sir !!!");
 }

 void main() {
     var total =get_add(2,3);
     print("total : $total");
     mul(2,3);
     var greeting=get_greeting();
     print("Greeting: $greeting");
      greetings();
 }
Show Output