Defining the Function in Dart
Dart provides us with the facility of using functions in its program.
In the above syntax:
- function_name: defines the name of the function.
- return_type: defines the datatype in which output is going to come.
- return value: defines the value to be returned from the function.
How to Call Functions in Dart?
In the above syntax:
- function_name: defines the name of the function.
- argument list: is the list of the parameters that the function requires.
Example 1: Complete function in Dart
int add(int a, int b){
// Creating function
int result = a + b;
// returning value result
return result;
}
void main(){
// Calling the function
var output = add(10, 20);
// Printing output
print(output);
}
Output:
30
Note: You must note that two functions can’t have the same function name although they differ in parameters.
Example 2: Function without parameter and return value.
void GFG(){
// Creating function
print("Welcome to w3wiki");
}
void main()
{
// Calling the function
GFG();
}
Output:
Welcome to w3wiki
Note: You must note that you can also directly return string or integer or any output of expression through the return statement.
Dart – Functions
The function is a set of statements that take inputs, do some specific computation and produces output. Functions are created when certain statements are repeatedly occurring 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.
Contact Us