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).
- Function with no arguments and no return type
- Function with arguments and no return type
- Function with no arguments and return type
- 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.
Learn more about Dart and Flutter
- List in Dart
- Abstract class and Abstract Methods in Dart
- Interface in Dart
- Constructors in Dart
- Arrow Function in Dart
- User Defined Function in Dart
- Functions in Dart
- Switch case in Dart
- Conditionals in Dart
- Operators in Dart
- Keywords in Dart
- Variables in Dart
- Data Types in Dart
Top comments (0)