loading...
Cover image for Types of function in Dart

Types of function in Dart

jay_tillu profile image Jay Tillu😎 ・2 min read

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
}

Sample Program

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

Output
Jay Tillu

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;
}

Sample Program

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

Output
20

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
}

Sample Program

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

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

Output
200

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;
}

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

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

Wanna get in touch with me? Here are links. I’ll love to become your friend. 😊
Twitter
Facebook
Instagram
Medium
or just mail me at jayviveki13@gmail.com

Posted on by:

jay_tillu profile

Jay Tillu😎

@jay_tillu

🌏 विधायास्य धमॅस्य संरक्षणम् 🌍 ☀️ Society before Self ☀️ 🙏🏻 Humanism by Religion 🙏🏻 💻 Work for Mankind 💻 🌐 Believes in वसुधैव कुटुम्बकम् 🌐

Discussion

pic
Editor guide