Introduction
In this article, we will be learning about the main() function of the dart. We have learned about the functions of the dart. If you haven't, go to this link. After we have written our code and when we come to the execution stage, the main() function is a point from where our execution starts. The main() function in our whole code is a top-level function and is also an essential and crucial part.
Inside the main() function, we write our whole logic, we call the other functions, declare our variables and write user-defined executables statements.
Return type: return type of main() function is void.
Parameter: it can have an optional parameter of type List<String>. It is used when we want to control our program from the outside, or we are coming from some other source.
Syntax:
void main()
{
//Code
}
//OR
void main(List<String> args)
{
//Code
}
Examples of main() Function
Let us try out some examples to get a more rigid knowledge of the main() function.
Example: In this example shows a simple main() function.
Code:
void main()
{
//printing the simple statement.
print("Controller is in the main() function.");
}
Output:
Controller is in the main() function.
Example: In this example, we show how to call another function.
Code:
void otherFunction()
{
print("Now, controller is in the otherFunction().");
}
void main()
{
//printing the simple statement.
print("Controller is in the main() function.");
//calling other function from main() function.
print("Calling the otherFunction(), but still in the main() function.");
otherFunction();
}
Output:
Controller is in the main() function.
Calling the otherFunction(), but still in the main() function.
Now, controller is in the otherFunction().
Example: In this example, we show how to call multiple functions.
Code:
int fun2(int x, int y)
{
//function to calculate the subtraction
//returing the subtraction
return (y - x);
}
int fun1(int x, int y, int z)
{
//Storing the result of subtraction.
int sub = fun2(x, y);
//returning the result.
return (sub * z);
}
void main()
{
//now see an example of nested function calling.
int x = 10, y = 20, z = 30;
int res = fun1(x, y, z);
//We will apply (y-x)*z on our variables and store result in res.
print("The result after applying operation: $res");
}
Output:
The result after applying operation: 300
Explanation: Here from the main() function we are calling a function fun1(), which afterwards calls a function fun2(). fun2() returns value to fun1() and fun1() returns value to main() function.





