×

main function in Dart

The main( ) function is a predefined method in Dart that is also known as the entry-point of the program. The compiler begins the execution only when it comes across the main( ) function. It is the most important function of the Dart programming language. This function is responsible for the execution of all the library functions, user-defined statements and user-defined methods.

 All the data variables and functions for the execution are defined inside the main( ) function only. An important point to remember here is that the main( ) function can only be used once in the program. The main( ) function may or may not return any value. Similarly, it may or may not have arguments as the parameters.

Given below is the syntax of the main( ) function :

void main( ) 
{
    // Body of the main( ) function which can contain variable declaration and method definition
}

Also, the main( ) function can have optional parameters as List< String > that act as arguments to the function. These are also known as command-line arguments.

Consider an example that explains the concept of main( ) function :

Example 1:

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

main( )
{
    print( " This is the main( ) function ! " ) ;
}

Output :


This is the main( ) function !

main( ) function with return value

We can make the main( ) function return some value also. At times the function returns some value based on the computation inside the function. The return statement comprises of the ‘ return ’ keyword and the variable that holds the value to be returned to the function call statement.

In case there is no return statement, then the function returns null. It is completely optional and as per the programmer whether to add the return statement or not. An important point to remember is that a function can only have one return statement.

Given below is the syntax :

return_type main( )   
{  
   // body of the function   
  return value ;  
}  

In the above syntax, the function contains a return type as well which can be any valid data type of Dart language. The value to be returned must match with the return type of the function. The “ return value ; ” statement refers to the returning of the value held by the variable ‘ value ’.

Consider the example that explains the concept of return value in main( ) function :

Example 2 -

int main( ) 
{  
  print( " This is the function returning an integer value ! " ) ;
  
  return 0 ;
}  

Output :

This is the function returning an integer value !

Example 3 :

Consider another example wherein a function returning some value is contained inside the main( ) function :

void main( ) 
{  
  int add( int a, int b ) 
    {  
        int sum = a + b ;  
        return sum ;  
    }  
  print( " The sum of these two numbers is : ${ add ( 40,50 ) } " ) ;  
}  

Output :

The sum of these two numbers is : 90

main( ) function with arguments

The main( ) function can accept arguments also as its parameters but they have to be of type List < String >. In this case, the program must be executed through command prompt to pass arguments to the main( ).

Given below is the syntax :

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

Consider another example that explains the above concept :

Example 4 :

void main( List< String > arguments ) 
{
  print( " The length of arguments is : " ) ;
  print( arguments.length ) ;


  print( " List of arguments is : " ) ;
  print( arguments ) ;
}

Output without using command prompt :

The length of arguments is : 
0
List of arguments is : 
[ ]

Output using command prompt :

To pass arguments to the main function, use the following command :

dart program_name.dart [ Argument 1 ], [ Argument 2 ], . . . . . . . . . . [ Argument n ]

Write this command in the command prompt as follows :

main( ) function in Dart
The length of arguments is : 
2
List of arguments is : 
[ 5, 10 ]

Related Topics

Dart Comments

Comments are the statements that are used to enhance the readability and understandability of the code. The compiler does not execute these lines. It simply ignores these comments when it scans...

2 minutes read.

Dart Construtors

Constructors are the very important concept in any programming language. They are the special functions created in the classes to allocate memory to the objects of that class when created...

4 minutes read.

Dart Generics

Dart Generics is similar to Dart collections, which are used to store homogeneous data. As we have discussed in Dart's features, it is a language with types being optional. By default,...

4 minutes read.

Dart super keyword

The ' super ' keyword is used to refer to the immediate parent class object of the currently in-use child class. Using this keyword, we can invoke the superclass (...

6 minutes read.

Dart Objects

Apart from the built-in data types, Dart offers some other data types that have a special role to play. One of them is Objects.  Objects Objects are the foundation of the...

3 minutes read.

main function in Dart

The main( ) function is a predefined method in Dart that is also known as the entry-point of the program. The compiler begins the execution only when it comes across...

3 minutes read.

C++ vs Dart

Difference between C++ and Dart C++ is an object-oriented programming language developed in 1980 by Bjarne Stroustrup. It has wide real-world applications with various in-built functions and a very vast library...

1 minute read.

Type System in Dart

Dart language is a type safety enabled language that uses static and dynamic type checking to match the value of the variable with its data type at the compile-time. This...

4 minutes read.

Dart Packages

Every programming language has some in-built functions stored inside the header files that makes the task much easier for the programmer. The Dart Packages refer to the compilation of a...

4 minutes read.

Dart Basics

Dart, as earlier mentioned multiple times, is very similar to C, C++ and Java. It is an object-oriented, garbage collecting and class-based programming language. Let’s look further and see what Dart has...

10 minutes read.

Dart If-else-if statement

If - else - if statement enables to check the set of test expressions that evaluate to Boolean True or False, and based on this true or false the particular...

2 minutes read.

Dart Miscellaneous types

Some Other Data types in Dart Apart from the data types studied so far, we have three other data types also which are as follows: NeverDynamicVoid Let us understand each one in detail, Never...

3 minutes read.

Soundness in Dart

What is soundness? Soundness ensures that the program never comes across any invalid state that may lead to abrupt termination of the program. As its name suggests it ensures perfect type...

4 minutes read.

Dart Iterable

Iterable is a collection of elements that are accessed sequentially. These elements are accessible using the iterator getter and stepping through the values using this getter. Let us understand the setting...

6 minutes read.

Assert in DART

In any programming language, resolving error is the most unwanted and tedious task. At times it becomes very difficult to find the error in the large code. Dart offers an...

3 minutes read.

Dart Types of Functions

Functions are the set of statements intended to perform a specific task. They accept some input from the user, perform the specified computation and generate the output. They are very...

4 minutes read.

Dart Arithmetic Operators

Arithmetic Operators comprises of all the operators that are used to perform arithmetic operations such as addition, subtraction, multiplication, division, etc. They are binary operators that work upon two operands. Consider A...

2 minutes read.

Unit Testing in Dart

Typing testing ensures that your app keeps working as you add more features or change existing functionality. Unit testing helps to confirm the behavior of a single function, method, or...

4 minutes read.

Dart Runes and Graphemes

Runes and Graphemes data types in Dart Runes and Graphemes Runes are the special string of Unicode UTF-32 bits that represent the special syntax. Unicode defines a unique numeric value for each...

2 minutes read.

Dart URIs

The Uri class supports encoding and decoding of strings with the help of functions to be used in URIs ( which may also be known as URLs ). These functions...

6 minutes read.