Types of Function in Dart

Types of Function in Dart

In Dart, one function can only return one type. And both return_type and return_value must be the same.

·

5 min read

Let’s discuss the types of functions. Remember that functions 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-defined 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

1. Function with no arguments and no return type

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.

Syntax

void function_name(){
  // Statements
}

Sample Program

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

Output
Jay Tillu

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

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

4. Function with arguments and with return type

Congrats!! now our function has arguments and return types 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

Conclusion

In this discussion, we explored the four main types of user-defined functions based on arguments and return types in Dart. Understanding these types is crucial for writing efficient and organized code.

  1. Function with no arguments and no return type: These functions are defined without any parameters and do not return any value explicitly. If no return type is specified, Dart considers it as void. It is a good practice, however, to explicitly specify void to indicate that the function does not return any value. These functions are useful when we need to perform specific tasks or actions without needing to return a result.

  2. Function with no arguments and return type: In this category, the function doesn't take any arguments, but it has a return type. It performs a task and returns a result of the specified return type. These functions are handy when we need to compute a value or perform a calculation without requiring any external inputs.

  3. Function with arguments and no return type: Here, the function takes one or more arguments (parameters) but does not have a return type. These functions are beneficial when we need to perform actions or tasks based on the provided arguments without generating any specific output. It is essential to pass the correct number and type of arguments while calling these functions to avoid runtime errors.

  4. Function with arguments and with return type: This type of function combines the best of both worlds. It accepts one or more arguments and also specifies a return type. These functions are versatile and widely used in Dart programming. They allow us to perform computations or operations based on provided inputs and then return a result, which can be further utilized in the program.

In conclusion, mastering the different types of user-defined functions in Dart empowers programmers to design elegant and efficient code structures. Choosing the appropriate function type for a given situation enhances code readability, reusability, and maintainability. As developers, we must carefully consider the requirements of our programs and choose the right function type to achieve the desired functionality. So, keep these function types in mind and leverage the full potential of Dart's user-defined functions in your future projects. Happy coding!

So, guys, that’s it for types of functions. Please explore it, practice it and try to understand it conceptually. These all are language fundamentals, if you have a strong understanding of them, you will feel right at home while learning Flutter. These concepts are also important for the logic building.

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

Feel free to let me know if I miss something. Till then Keep Loving, Keep Coding. And I’ll surely catch you up in the following article.

💡
Subscribe To My Newsletter For More Such Content.

Learn More about Dart and Flutter

Follow me for more such content.

Did you find this article valuable?

Support Jay Tillu by becoming a sponsor. Any amount is appreciated!