×

Dart Syntax

In the previous tutorial, we coded our first program in Dart on various platforms understanding the basic functionality of each line of code. 

Let us understand the syntax for Dart language in detail: 

1. Header files or libraries:

There are various inbuilt functions provided by the Dart language whose definitions are stored in header files or libraries. Whenever we use any in-built function in our program, we need to import the respective header file or library. Header files are written before any function at the beginning of the program.

For example, standard input function stdin.readLineSync( ) has its definition in dart : io header file. 

In order to import a header file, follow this syntax:

import 'dart:io';

2. The main( ) function is the entry point for all dart applications. The execution of a program begins from here.

It is written as follows:

return type main(function arguments)

{

                 Body of the function

     }

Return type refers to the type of value returned by main( ) function. Usually, it is void, int or double. Function arguments are the variables passed to the function. Generally, main( ) doesn't have any arguments. 

For example,

void main( )

{

  print( ‘Hello World!’ );

}

Output:

Hello World!

3. The beginning and end of the program's line of codes are denoted by the curly braces { }. Group of program statements shall always be enclosed within these braces, also known as a block. 

For example,

{

 Statements;

}

4. Every statement of the Dart program is terminated by a terminator or semi-colon ";". It indicates that the statement has ended here.

5. The most integral parts of any language are its variables, methods, classes, and functions. The name that defines these parts is called an identifier. It is a sequence of letters, digits, and some permissible special characters like underscore "_" or dollar "$". There are a few rules to write identifiers in a Dart code which are as follows:

  • Dart is a case-sensitive language, and so are the identifiers.
    For example, Area and area are two different identifiers.
  • Blank spaces are not allowed in the identifiers.
    For example, ‘area circle’ is an invalid identifier while ‘areacircle’ is a valid identifier.
  • Identifiers cannot be the keywords, which are the reserved words of a programming language, and they must be unique.
    For example, case, const, set, etc. are the invalid identifiers as they are the keywords provided by Dart.
  • Identifiers can not include any special character except underscore "_" or dollar "$".
    For example, area_circle, _area, $area, num$, area$, and area$circle are valid keywords while area-circle, num*, and num% are invalid identifiers.
  • The first character can only be an alphabet (lower or upper case) or permissible special characters such as, dollar ‘$’ and underscore ‘_’. But two successive underscores ‘__’ are not allowed.
    For example, _radius, Dividend, $rem are valid keywords while, 5rem, __radius are invalid keywords.

 These are the syntaxes for the declaration of variables and functions : 

1. Variables :

datatype variable_name;

Here, data type refers to the type of value that will be stored in variables like int, double, string, etc. 

For example,

int num;

2. Functions :

return type function_name( arguments )

{

// Body of the function

}

Here, return type refers to the type of value returned by the function. The function name is followed by round parentheses that contain the list of arguments which are variables passed to the function. Note that parameters are optional in the function.

For example,

int area( int r )

{

     int area = 3.14 * r * r;

     return area;

}

Whenever we use user-defined functions in Dart, we need to call them in main( ) to execute them. The statement that calls the function is known as a call statement. If the return type of function is void, then the call statement includes only the function name and variables passed to it as follows:

function_name( variable );

And if the return type is any other type than void, then the call statement is as follows: 

variable = function_name( variable ).

This is so because the value returned by the function has to be received by a variable, calling the function.

In both the cases, variable inside the parentheses refers to the variable we pass as an argument in the function. 

For example:

Consider the following code,

import 'dart:io';

//function to calculate remainder

int compute( int a, int b )

{

  int c = a % b;

  return c;

}

//main function to test

void main( )

{

  int rem;

  print( 'Enter the value of a : ' );

  int a = int.parse( stdin.readLineSync( )! ); // accepts integer value from the user

  print( 'Enter the value of b : ' );

  int b = int.parse( stdin.readLineSync( )! );

  rem = compute( a, b ); // function call statement

  print( 'Value of remainder is $rem' );

}

Output:

Enter the value of a :

10

Enter the value of b :

4

Value of remainder is 2

6. print('Text message goes here');

print( ) function prints the statements or values on the output screen. The text or value to be printed is enclosed within the single or double quotes. 

To print the value of a variable, we use string interpolation '$variable_name'

For example, 

void main( )

{

    float area = 125.5;

    print( 'Value of area is $area' );

}

Output:

Value of area is 125.5

Related Topics

Dart Built-in Data Types

A data type defines the type of value a variable can hold, such as integer, double, or string.  Dart supports two data types:  1. Built-in data types : These are the data...

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

Synchronous Generator Dart Generator is a unique function that allows us to generate price sequences. Generators return values when needed; means that the value is generated if we try to duplicate...

5 minutes read.

Dart Switch Case Statement

In the case of if-else statements, we prefer not to use the if-else ladder when there are many test conditions to be evaluated. In such a situation, it is preferable...

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

Dart Syntax

In the previous tutorial, we coded our first program in Dart on various platforms understanding the basic functionality of each line of code.  Let us understand the syntax for Dart language...

4 minutes read.

Dart Constants

Dart Constants are the objects or variables whose values can’t change or modify during the execution of the program. Their use case is when we want a particular value to...

2 minutes read.

Dart Object-Oriented Concepts

Dart is a programming language that supports object-oriented programming. It is focused on the objects that are real-world entities and supports all the concepts of OOPS, such as objects, classes,...

4 minutes read.

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.

Dart lists data type

The most important data type in any programming language is an ordered list of elements, an array. Dart lists resemble JavaScript array lists. Example,               var...

7 minutes read.

Dart Basic Program

Dart provides various convenient platforms to code and compile the program in Dart language. Some of them are: 1. Using IDE = Visual Studio code is a text editor with IDE....

3 minutes read.

Dart Logical Operators

Logical operators are the operators that combine two or more conditions, and accordingly return a Boolean value : true or false. Assume the value of variable A is 25 and B...

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

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.

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.

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 Future and Stream

Before understanding Future and Stream in Dart, we need first to get familiar with two terms : Synchronous and Asynchronous Programming.  In synchronous programming, a single operation is handled at a...

9 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 Single-Page Application Architecture

In a single page application architecture, the source code for a single web page loads the entire application. The responsibility of building the user interface and requesting data from the...

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