Home > General > Declaration

Declaration

Strongly typed programming languages are ones which require that you declare the data types of each variable before they can be assigned a value. The compiler can then check for errors more easily as if variables String1 and String2 are being divided and both have a string data type, this will result in an illegal operation as it is not possible.

Declaring variables
Above any instructions which you write, you first need to include the declaration block where you will declare all of your variables. Below is the basic layout of a complete program:

program
{program name here}
uses
{library which pascal uses}
const
{constant declaration block}
var
{variable declaration block}
function
{any functions that are used}
procedure
{any procedures that are used}
begin
{start of main program block}
end.
{end of main program block and program}

The next example shows an exemplar declaration block which declares three variables:

var
     number1, number2  : integer;
     myPhrase          : string;

As in the example above, variables sharing the same data type can be separated by commas and after the variable(s) comes a colon which is followed by the data type. It is also good practice to align up all colons and then data types respectively for easing reading.

If you declare variables, as in the above layout of a complete program, in the variable declaration block which is above any procedures or functions, these are global variables, of which can be accessed anywhere in the code. However they are not always desirable as they could be accidental changed which is why local variables exist. Local variables are declared within a routine, such as a function or a procedure.

Categories: General Tags: