![]() |
VOOZH | about |
The function is a set of statements that take inputs, do some specific computation, and produce output. Functions are created when certain statements repeatedly occur in the program, and a function is created to replace them. Functions make it easy to divide the complex program into smaller sub-groups and increase the code reusability of the program.
There are four types of functions in Dart as mentioned below:
In this function, we do not give any argument and expect no return type. An example can help explain it better.
Output:
This is the best website for developers:
GeeksForGeeks
So myName is the function that is void means it is not returning anything and the empty pair of parentheses suggest that there is no argument that is passed to the function.
Basically, in this function, we don't give an argument but expect a return type.
Output:
GeeksforGeeks is the best website for developers which costs : 0/-So myPrice is the function that is int means it is returning int type and the empty pair of parentheses suggests that there is no argument which is passed to the function.
Basically, in this function, we are giving any argument and expect no return type.
Output:
GeeksforGeeks is the best website for developers which costs : 0So myPrice is the void function, meaning it is not returning anything, and the pair of parentheses is not empty this time, which suggests that it accepts an argument.
Basically in this function, we are giving an argument and expect return type. It can be better understood by an example.
Output:
600So mySum is the function that is int means it is returning int type and the pair of parentheses is having two arguments that are used further in this function and then in the main function we are printing the addition of two numbers.
Dart functions play a crucial role in organizing code efficiently. The choice of function type depends on the specific use case:
By following these principles, you can write clean, reusable, and efficient Dart code.