×

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

Dart Concurrency

Dart concurrency allows us to run multiple or multiple components of a system at once. It issues several commands at once. Dart provides Isolates as a tool for performing compliance...

2 minutes read.

Dart Type Test Operators

Type Test Operators in Dart These operators are used to check the types of expressions at runtime. Dart is a typed language, and we often want to assert that a value...

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

Inheritance is a promising concept of any programming language. It is a property by which a class inherits the property of another class. Moreover, the class deriving the properties of...

5 minutes read.

Abstract Classes in Dart

Abstract classes in Dart are those classes that only contain abstract methods  (methods that do not contain any implementation). These classes are specifically designed for the purpose of inheritance. We...

4 minutes read.

Dart Booleans

Booleans Dart has a data type named ‘bool’ to represent Boolean values : true and false, which are compile-time constants. ‘True’ or ‘False’ cannot be assigned with values 1 or 0. Example, bool...

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

Dart provides an in-built data type ‘Numbers’ that provides two types of values: intdouble 1. int data type int data type supports integer values, and their size or values range is platform-dependent. Integers...

4 minutes read.

Dart HTML DOM

All web pages can be viewed as objects and exist within the browser window. We can access a web page using a web browser and it needs to be connected...

3 minutes read.

Dart Enumerations

Enumeration data type in Dart In simple terms, Enumerations are referred to as named constant values. Constant values are declared as enumerations using the ‘enum’ keyword.  Implementation =  enum enum_name {    ...

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

Dart - extends, with and implements Keywords

The application development in Dart programming language using the Flutter framework, regularly experience different usage of the implements, extends and with keywords. Dart has full support for inheritance that is...

5 minutes 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 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 Symbols

Symbols data type in Dart A symbol is an object that is the representation of an operator or identifier in Dart. These are compile-time constants.  They are used in APIs that refer...

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