×

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 Loops

Looping refers to repeating or reusing the same lines of code repeatedly until a particular test condition evaluates to true. It is also known as iterating.  It is effectively used when...

4 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.

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.

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 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 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.

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.

Dart Exception Handling

Exception Class – An exception is any runtime error that leads to abrupt termination of the program. It helps in addressing the error programmatically. It is aimed to be caught...

4 minutes read.

Dart Variables

Variables are used to store the value of a particular data type referred to in the whole program by a name or identifier. They refer to a memory location as...

5 minutes read.

Interfaces in Dart

An interface in Dart refers to the syntax or blueprint that any class must adhere to. It basically defines the array of methods available on the object. It provides the...

3 minutes read.

Dart Queues

A queue is a user-defined collection of data. It is based on the FIFO principle ( First In First Out ) which implies that the element to be inserted first,...

3 minutes read.

Dart Operators Precedence and Associativity

Precedence of operators determines the order in which the operators are evaluated if they are grouped together in a sentence. If two operators share an operand then the one with...

5 minutes read.

Builder Classes in Dart

Before understanding builder classes, let us understand about Flutter that makes the best use of the builder classes. Flutter is actually not a programming language, it is a software development...

6 minutes read.

Dart Function

A Dart function is a collective line of code that are targeted to perform a specialised task. Such lines of code (function) can be reused whenever we need to perform...

6 minutes read.

Dart Sets

Sets data type in Dart A set is an unordered collection of items that are unique. Dart infers that sets tale strings as values. Example, var languages = { ‘Dart’ , ‘Flutter’, ‘Python’,...

3 minutes read.

Dart - Control Flow Statements

The control flow statements are used to define the control on the flow of the program. Dart programs are executed in sequential order i.e., the order in which the programming...

1 minute 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 Keywords

Keywords are the reserved words of a programming language, and they have a special meaning that comes defined by the language. These cannot be used as an identifier, and all...

1 minute read.

Dart Bitwise and Shift Operators

The Bitwise operators are the operators that perform different operations bit by bit on the value of the two operands. List of Bitwise Operators in Dart Sr. Operators Description 1. & ( Binary AND ) It returns...

2 minutes read.

Callable Classes in Dart

Just like the functions, we can also call the instances of classes in Dart. Such classes are also known as “callable ” classes. We need to use the “call( )”...

3 minutes read.