Open In App

Dart – main() Function

Improve
Improve
Like Article
Like
Save
Share
Report

The main() function is a predefined method in Dart. It is the most important and mandatory part of any Dart Program. Any Dart script requires the main() method for its execution. This method acts as the entry point for any Dart application. It is responsible for executing all library functions, user-defined statements, and user-defined functions. 

Syntax of main() function:

void main()
{
    //main() function body 
}

The main function can be further structured to variable declaration, function declaration, and executable statements. The main function returns void. Also, optional parameters List<String> may be used as arguments to the function. These arguments may be used in case we need to control our program from outside. 

Example 1:

The following example is a basic example of how the main function is the entry point of a Dart program.

Dart




main(){
    print("Main is the entry point!");
}


Output:

Example 2:

The following example shows how we can pass arguments inside the main() function.

Dart




main(List<String> arguments){
     //printing the arguments along with length
     print(arguments.length);
     print(arguments);
}


We run the app using the following code(if main.dart is the name of the saved file):

 dart main.dart Argument1 Argument2

Output:


Last Updated : 17 Feb, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads