DATA

CONTROLS: (1) Data = Data Attributes; (2) DAI = Declarations, Assignment and Instantiation; (3) Glo/Loc = Global and Local Data; (4) Var = Variables; (5) Const = Constants; (6) Anon = Anonymous Data Items; (7) Ren = Renaming and (8) Over = Overloading.

CONTENTS



DATA ATTRIBUTES

DATA - "that which is given". A data item has a number of attributes:

  1. An Address.
  2. A Value which in turn has:
  3. A set of Operations that may be performed on it.
  4. A Name to tie the above together (also referred to as a label or identifier).

Address


Value


Operations

The type of a data item also dictates the operations that can be performed on it. For example numeric data types can have the standard mathematical operations performed on them while string data types cannot.


Names

To do anything useful with a data item all the above must be tied together by giving each data item an identifying name (sometimes referred to as a label or an identifier).


Summary

Data Items comprise: (1) a name, (2) an address and (3) a value. It is important to distinguish between these:

NAME, ADDRESS, VALUE

The type of a data item dictates:

Data items can be classified as being either global or local data items, and as being either variables or constants. The nature of a data item is defined through a data declaration.




DATA DECLARATIONS, ASSIGNMENT AND INITIALISATION

Data declaration

A data declaration introduces a data item. This is typically achieved by associating the item (identified by a name) with a data type. Examples in Ada, Pascal and C:

NUMBER: integer;     NUMBER: integer;     int number;

In some Imperative languages we can specify the possible values for a data item, in which case the compiler may deduce the type of a data item according to the nature of these values. Examples in Pascal:

NUMBER: 1..10;               LETTWER: 'A'..'C';

Here the first item will be considered to be an integer (because the range of possible values for the item are integers), and the second will be considered to be a character (because the range of possible values for the item are characters).

Assignment

Assignment is the process of associating a value with a data item. Usually achieved using an assignment operator. Examples in Ada, Pascal and C:

NUMBER:= 2;          NUMBER:= 2;          number = 2;

Initialisation

Initialisation is then the process of assigning an initial value to a data item. Some imperative languages allow this to be done on declaration (e.g. C and Ada) others do not (e.g. Pascal). Examples in Ada and C:

NUMBER: integer:= 2;                         int number = 2;

Ordering of declarations

In some imperative languages (notably Pascal) declarations must be presented in a certain order, e.g. constants before variables. In Pascal constants are grouped together in a CONST section, followed by variables grouped together in a VAR section.

Multiple

Many imperative langauge (Ada and Pascal, but not C) support the concept of multiple assignment where a number of variable can be assigned a value (or values) using a single assignment statement. Examples in Ada and Pascal:

NUMBER1, NUMBER2:= 2;                        NUMBER1, NUMBER2:= 2, 4;

Note that in Pascal we do not have to assign the same value to ecah varaibale when using a multiple assignment statement. In Pascal we can also write:

NUMBER1, NUMBER2 := NUMBER2, NUMBER1;

which has the effect of "doing a swap".




GLOBAL AND LOCAL DATA

Data items have associated with them:

  1. A life time - the period during the running of a program when they can be said to exist.
  2. A visibility - the parts of the program from where they can be accessed ("seen").

The nature of the life time and visibility of a data item is dictated by what are referred to as scoping rules. In this respect there are two types of data:

  1. Global data, which has a life time equivalent to an entire program and is visible from anywhere within that programme.
  2. Local data, whose life time and visibility is in some way limited.



VARIABLES

Uninitialised Variables




CONSTANTS

It is sometimes desirable to define a data item whose value cannot be changed. Such data items are referred to as constants and are usually indicated by incorporating a predefined key word into the declaration. Examples in Ada and C:

I: constant integer:= 2;                const int i = 2;

Conceptually we can think of a constant as a data item comprising only of a name and a value (i.e. no address):

NAME, VALUE

However it should be appreciated that what we are doing here is telling the compiler to "flag" the data item as a constant (i.e. we are only instigating software protection). Theoretically we can still change the bit pattern representing the value.




ANONYMOUS DATA ITEMS

Often we use data items in a programme without giving them a name, such data items are referred to as anonymous data items. Examples include:

4 + (5*6)                       x + (5*6)

The (5*6) is an anonymous data item. The significance is that anonymous data items cannot be changes or used again in other parts of a program. Conceptually we can think of an anonymous data item as a data item without a name, consequently we cannot access its address (or its value).

ADDRESS, VALUE

To summarise the above:




RENAMING

It is sometimes useful, given a particular application, to rename (alias) particular data items, i.e. provide a second access path to it. This is supported by some imperative languages such as Ada (but not C or Pascal). Ada Example:

I: integer = 1;
ONE: integer renames I;

Here we have declared a data item integer and then allocated a second name (ONE) to the item. Thus, conceptually, renaming creates a data item with more than one name (but only one address and consequently only one value).

ADDRESS, VALUE

It is dangerous to rename variables. Consider the following Ada programme:

with CS_IO; use CS_IO;

procedure RENAME is
        ITEM : integer;
        ONE  : integer renames ITEM;
begin
        get(ITEM);

        put("ITEM = "); put(ITEM); new_line;
        put("ONE = "); put(ONE); new_line; new_line;

        ITEM:= ITEM*10;
        put("ITEM = "); put(ITEM); new_line;
        put("ONE = "); put(ONE); new_line; new_line;

        ONE:= ONE*10;
        put("ITEM = "); put(ITEM); new_line;
        put("ONE = "); put(ONE); new_line;
end RENAME;
rename

Input integer ITEM and .

Note that any change made to data item ITEM results in an identical change to data item ONE (and vice versa). This is because both names represent the same data item.




OVERLOADING
 5 + 3                   5 - 3
5.6 + 3.2               5.6 - 3.2
 5 + 3.2               5.6 - 3
5.6 + 3                   5 - 3.2



CONTINUE TES/NO

EMERGENCY SHUT DOWN PROCEDURE! Left lever: return to imperative home page. Right: continue.




Created and maintained by Frans Coenen. Last updated 03 July 2001