PROGRAM COMPOSITION

Time for a break! We can distinguish four levels of programme hierarchy:

Two other possible ingredients, which should be discussed here, are Generics and Abstract Data Types (ADTs).

CONTENTS

Choose a mug: (1) Blocks, (2) Routines, (3) Modules and Packages, (4) Programs, (5) Generics, or (6) Abstract Data Types (ADTs).




BLOCKS

FORMAL PARAMETERS


BLOCK STRUCTURED LANGUAGES


SCOPE RULES

Scope rules govern the visibility of data items (i.e. the parts of a program where they can be used). They also bind names to types. Where this is determined at compile time this is called static scoping. The opposite is dynamic scoping. Dynamic scoping is generally a feature of logic languages such as PROLOG and functional languages such as LISP. The advantages of static scoping is that it allows type checking to be carried out at compile time. Most imperative languages use static scoping.

Generally speaking, in imperative languages, the scope of a declaration commences at the end of the declaration statement and continues to the end of the block.


Ada Scope Rules

  1. Scope of declaration extends from where it is made to the end of the block in which the declaration is made.
  2. Anything declared in a block is also visible in all enclosed blocks in which it is not redeclared.
  3. Several items with the same name cannot be used in the same block, however several items with the same name can be declared in "different" blocks. In this case the declared quantities have nothing to do with one another (other than sharing the same name).
  4. Where a name is used in a block and reused in a sub-block nested within it, the sub-block declaration will override the super-block declaration during the life time of the sub-block. This is referred to as occlusion. The identifier which is hidden because of the inner declaration is said to be occluded.

Ada Example:

PRESS INTERACTIVE ADA NESTING DIAGRAM

C Scope Rules

The scope of a data item in C is governed by its storage class. Generally data items belong to one of two storage classes:

  1. extern (external) - used to define global variables visible throughout a program.
  2. auto (automatic) - used to define local variables (including formal parameters to functions) visible only inside a procedure (block). Local variables exist only while the block of code in which they are declared is executing. Note that where a local variable and a global variable share the same name the local variable overrides the global variable.

C also supports two other storage classes static and register, but these will not be discussed further here.


C Example:

PRESS INTERACTIVE C SCOPE RULES DIAGRAM


SUB-PROGRAMS (ROUTINES)

One of the most important issues that has to be addressed in the design of a programming language is the support it gives to the control of complexity. An important technique is the division of a program into "chunks", called routines or sub-programs, that will allow the programmer to think at a more abstract level. At its simplest a routine can be equivalent to a block. More specifically a routine can be defined as a collection of declarations and statements that must be invoked explicitly (routine invocation). Note that after invocation control is returned to the point immediately after the invocation. An additional advantage is that routines can be stored in libraries for reuse.

Further Issues involved include: