Blocks are just a bunch of code that can be grouped together to perform some task.

Writing a Block


In haXe blocks are enclosed in { and }. Lets see how -
class Main
{
 
  static main()
  {
    trace("Hello for outside the block");
    {
      trace("Hello from ");
      trace("inside the block");
    }
  }
 
}
Notice those { and } in the main() function. It contains the command trace() to print the text to the screen.

Where Blocks are Used


The basic function of blocks is to group a number of lines of code together to make them function as one line of code. This is useful when we need to set the body of function,etc. In fact, if you have observed, the main() function itself has a block in it. For example,
static main()
{
  trace("hello");
  trace("world");
}

The two trace statements are grouped together in a single block by using the braces. Then this block in assigned to the main function as its code body. This is the primary use of blocks. It helps in grouping statements together into one so that it can be assigned to something.
As you have seen, the whole body of the class is actually within a block because it is enclosed within two braces.

Blocks Hold Value


Unlike in languages like C or Java, blocks in haXe have the ability to return or hold a value. What do I mean by this ? Lets see. Look at this example where we find the sum of two numbers.
class Sum
{
 
  static main()
  {
    var a = 10;
    var b = 10;
    var c = a + b;
  }
 
}
Simple enough, but what if we could use blocks to write this in another way.

class Sum
{
 
  static main()
  {
    var c =
   {
     var a = 10;
     var b = 10;
     a+b;
   }
 }
 
}
Can you understand this ? We group the statements in which declare a and b into one block. We also make the block return a value, .i.e, a+b. This last statement specifies the value that the block will return/hold. Now, since the block holds this value we can assign the value to c. This gives it the value 20.

Now, using blocks in this simple case is kind of useless. You don't actually need to declare a and b, you just need to say var c = 20. But, this example teaches you how to make blocks return a value. It may not look useful here, but when we use things like if statements, this concept will help make the code better not to mention cooler.