Types of function in Dart
Tags: dart, todayilearned, beginners, code
Published: 2019-09-16 02:29:58

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

```dart
void function_name(){
  // Statements
}
```

####Sample Program
```dart
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
```dart
return_type function_name() {
  // Statements
  return value;
}
```

####Sample Program
```dart
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
```dart
function_name(args1, args2, ...argsN){
  // Statements
}
```
####Sample Program
```dart
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
```dart
return_type function_name(args1, args2, ...argsN) {
  //statements
  return value;
}
```

####Sample Program
```dart
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
* [Fuchsia OS official site](https://fuchsia.dev/)
* [Dart official site](https://dart.dev/)
* [Flutter official site](https://flutter.dev/)


Jai Hind, Vande Mataram 🇮🇳

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

- [My Site] (https://www.jaytillu.in)
- [My Blogs] (https://blogs.jaytillu.in)
- [Twitter](https://twitter.com/jay_tillu)
- [Facebook](https://www.facebook.com/jaytillu.1314/)
- [Instagram](https://www.facebook.com/jaytillu.1314/)
- [Medium](https://medium.com/jay-tillu)
- [LinkedIn](https://www.linkedin.com/in/jaytillu/)
- [Github](https://www.github.com/jay-tillu)
- [Stack overflow](https://stackoverflow.com/users/8509590/jay-tillu)
or just mail me at jd.tillu@gmail.com