next up previous contents
Next: References and Dereferences Up: The MPD Programming Language Previous: User defined types   Contents

Subsections

Variables and constants

A variable is a placeholder for a value of a specified type that may change during the lifetime of the variable. A constant is a placeholder which value does not change. Variables may be initialized a a part of the declaration of the variable; a constant must be initialized when declared.

Variables

A variable declaration is built up by the type of the variable followed by a comma separated list of variable definitions.

variable_type variable_definition, variable_definition, ...

A variable definition has one of two forms. The first form initializes the variable with the result of evaluating the expression. The second form introduces an uninitialized variable.

var_name = expr           or    var_name

The variable name has one of two forms. The first form defines a simple variable, while the second form defines an array.

var_id             or                  var_id dimensions

The dimensions give a range for each dimension of the array. The form of the array dimensions is the same as for type declarations.

Some examples of variable declarations are the following:

int i, j, k                   # three integer variables
int front = 0, rear = 0       # two initialized integers
string[80] line               # string of up to max 80 chars 
person k = person("karl", 92) # initialized record 
int a[10]                     # an array of 10 integers
int char_count[char(0):char(127)]  
       # array of 128 ints, 7-bit integer indices.
bool boolean_matrix[N,N]      # a 2D matrix of booleans

Note that the number of elements of boolean_matrix references N, which must be declared previously and given a value.

Observe!

The fact that a variable is an array can be specified with the variable's name, as in the examples above, or with the variable's type. Thus, the following are equivalent to the declarations above with the same name.

[10] int a
[char(0):char(127)] int char_count
[N,N] bool boolean_matrix

Since a matrix may be seen as an array of arrays the boolean matrix may equivalently be defined as

[N] bool boolean_matrix[N]

More variations are possible. We refer the interested to the grammar for MPD.

Predefined Array Functions

Now that we have described the initialization of arrays we can give the two predefined functions on arrays:


lb returns the lower bound of its argument  
ub returns the upper bound of its argument  

On multidimensional arrays the function by default return the bounds of the first dimension. Bounds for other dimension may be obtained by specifying the desired dimension as a second argument, which must be an integer literal. Consider the following declaration:

int a[10]; real q['a':'z', -2:4]

The following are some values of lb and ub for these arrays:

expression value expression value
lb(a) 1   lb(q,1) 'a'
ub(a) 10   lb(q,2) -2
lb(q) 'a'   ub(q,1) 4
ub(q) 'z'   ub(a,1) 10

Constants

A constant is declared in the same manner as variables but preceded by the keyword const. Constants must be initialized at the point of their definition.

const const_type const_definition, const_definition, ...

The constant definition has only one form:

const_id = expr


next up previous contents
Next: References and Dereferences Up: The MPD Programming Language Previous: User defined types   Contents
David Sands 2003-09-05