Variables
Variable is an object whose value can be changed during the runtime. Every variable is declared under unique name which must be a valid identifier. This name is used for accessing the memory location occupied by a variable.
Variables are declared in the declaration part of the file or routine — each variable needs to be declared before being used. Global variables (those that do not belong to any enclosing block) are declared below the uses
statement, above the keyword begin
.
Specifying a data type for each variable is mandatory. Syntax for variable declaration is:
var identifier_list : type;
Here, identifier_list
is a comma-delimited list of valid identifiers and type
can be any data type.
For more details refer to Types and Types Conversions. For more information on variables’ scope refer to the chapter Scope and Visibility.
Pascal allows shortened syntax with only one keyword var
followed by multiple variable declarations. For example:
var i, j, k : byte; counter, temp : word; samples : array[100] of word;
External Modifier
Use the external
modifier to indicate that the actual place and initial value of the variable, function or procedure body, is defined in a separate source code unit.
For example, lets create a project which will calculate circle area and will have function and procedure definition in two different units, and a call to these routines in the third, separate unit.
So, the project will be consisted of the main unit, Main_Unit.mpas
and First_Unit.mpas
and Second_Unit.mpas
units.
In the Main_Unit
we will define routine called r_squared
(calculates radius squared). Also, both units must be included in the Main_Unit
:
program Main_Unit; uses First_Unit, Second_Unit; // Include both used units function r_squared(r : real) : real; // Definition of the r_squared routine begin result := r*r; end; begin CircleArea(); // CircleArea routine call end.
In the First_Unit
we will define and declare routine called pi_r_squared
(calculates pi multiplied by the radius squared):
unit First_Unit; procedure pi_r_squared(rr : real); // Declaration of the pi_r_squared routine implementation procedure pi_r_squared(rr : real); // Definition of the pi_r_squared routine var res : real; begin res := rr*3.14; end; end.
In the Second_Unit
we will make a call to the routines defined externally (r_squared
and pi_r_squared
). First of all, we must declare their prototypes followed with a external
modifier. Then, we can proceed to the routine call :
unit Second_Unit; procedure CircleArea(); function r_squared(r : real) : real; external; // Declaration of the r_squared routine (defined in Main_Unit) followed with a external modifier procedure pi_r_squared(rr : real); external; // Declaration of the pi_r_squared routine (defined in First_Unit) followed with a external modifier implementation procedure CircleArea(); // Definition of the CircleArea routine var res : real; begin res := r_squared(5); // r_squared routine call pi_r_squared(res); // pi_r_squared routine call end; end.
Variables and PIC
Every declared variable consumes part of RAM. Data type of variable determines not only allowed range of values, but also the space variable occupies in RAM. Bear in mind that operations using different types of variables take different time to be completed. mikroPascal PRO for PIC recycles local variable memory space – local variables declared in different functions and procedures share the same memory space, if possible.
There is no need to declare SFRs explicitly, as mikroPascal PRO for PIC automatically declares relevant registers as global variables of volatile word
see SFR for details.
What do you think about this topic ? Send us feedback!