Declaration Block Examples
Previous Topic  Next Topic 

Declarations can be surrounded by optional dec…enddec statements (which sometimes help with the readability of the code).   However the choice of whether to include dec..enddec keywords is up to the programmer:


This:


dec

    var a:10;

    var b:10;

enddec


is equivalent to:


    var a:10;

    var b:10;


Wherever a declaration_block is valid.



The declarations themselves can be single declarations like this:


var a:10;

var b:10;

var c:10;

var d[1..10]:8;


Or can be included in a single line as a comma separated list terminated by a semi-colon:


var a:10, b:10, c:10, d[1..10]:8;


Similiarly int types can be declared one to a line or listed on a single line separated by comma.   


int z;

int y;

int a,b,c,d[1..10];


and also the same for const types which can be decared:


const z= "abc";

const y=4;

const a= "123", b=3, c=-14;


int types, var types or const types cannot be mixed in a single comma separated list.


Arrays are declared by specifying the start and end indexes of the array in square brackets after the array variable name (separated by double dots).   For example :


var myarray1[1..10];

int myarray2[61..70];


This declares two arrays:  myarray1 which has ten elements with indexes ranging from 1 to 10 and myarray2 which has ten elements with indexes ranging from 61 to 70.     Elements from the array can be referenced in the code by specifying the index in square brackets:


a=myarray1[3];

myarray2[62]=4;



If an index is specified to an array that is outside of its index range then this will not be picked up during the compile, it will only be spotted at run-time when an error message will be logged to the error screen and to the error logs.    The reason for this is that the index to an array  can be any expression, for example:


a=myarray[a*b+4];


It cannot be established at compile time whether any expression is going to be a valid index to an array and therefore these errors are only picked up at run-time.


Constant declarations allow an identifier to be assigned a constant value which can be used instead of that value anywhere in the code.   This is typically used to make programs more readable and to allow easier maintenance of code.    


For example, in the following code it is clear that we are waiting for the event CS_INCOMING_CALL_DETECTED  which is easier to read than if we were checking that CCwait() was returning the value 2:


const CS_INCOMING_CALL_DETECTED=2;

int port,chan,x;


main

   port=0; chan=1;

   while(1)

       x=CCwait(port,chan,0);

       // This is easier to understand than using ‘if(x == 2)’

       if(x == CS_INCOMING_CALL_DETECTED)

            break;

       endif

   endwhile


  

endmain