Skip to main content
main-content

Über dieses Buch

MODULA-2 is a new programming language which was created by Niklaus Wirth of the Swiss Federal Institute of Technology (ETH) in Zurich. The lan­ guage is derived from PASCAL: it includes all aspects of PASCAL and some­ times improves on them. Moreover, MODULA-2 includes the important "mod­ ule" concept, as well as multiprogramming capabilities and a way of implemen­ ting low-level software in an elegant manner. In summary, MODULA-2 may be used equally well as a general-purpose programming language and as a system implementation language. MODULA-2 provides the programmer with a good way of writing high quality software. In particular, modules are powerful tools for achieving modularity, reliability, readability, extensibility, reusability and ma­ chine-independence. This book presents the complete MODULA-21anguage from the beginning. Each topic is presented by means of numerous examples and each concept is justified. The syntax of the language is explained using syntactic diagrams. This book is not a reference manual for MODULA-2, but a textbook from which the student can learn the language progressively. The most important con­ cepts (i.e. procedures, modules and data structures) are explained in great detail and methodological aspects are also emphasized. Beginning in the first chapter, the student may execute his/her own pro­ grams. Program examples in this book have been executed on several machines (APPLE II, IBM PC and VAX 11/780) and they may be taken as a basis for stu­ dents.

Inhaltsverzeichnis

Frontmatter

1. Introduction

Abstract
A computer is an electronic machine composed of many circuits. The physical machine is called the hardware. This is often viewed as analogous to a human being without a brain. In fact, the hardware on its own has only limited capabilities. To be powerful, a computer must be programmed. It has to receive and understand instructions. Moreover, many basic instructions must already be stored in the computer. These basic instructions are known as the basic software of the computer. Instructions to the computer cannot be entered randomly and without logic or rules. In fact, to program a computer it is necessary to use a language, called a programming language.
Daniel Thalmann

2. Numbers

Abstract
An integer number is defined as a sequence of digits. It cannot include any spaces, commas, apostrophes or decimal points.
Daniel Thalmann

3. Constants, Variables and Data Types

Abstract
When numbers are constant during the execution of a program and when they have a specific role to play, it is useful to give them a name. This is possible in MODULA-2 by means of constant declarations. A constant is defined by giving its name, or constant identifier, and its value as an expression involving numbers and constants.
Daniel Thalmann

4. The Boolean and CHAR Types

Abstract
Many things can be expressed in terms of two mutually exclusive states. For example, either I am at home or I am not; either I have enough money to buy the latest Porsche or I have not. It is therefore important to be able to define variables which can take precisely two values. Such variables are called logical variables or boolean variables (after the mathematician George Boole). In MODULA-2 there is a predefined type BOOLEAN. Any variable of type BOOLEAN can take one of the two values FALSE or TRUE, where FALSE and TRUE are standard identifiers.
Daniel Thalmann

5. Basic Control Statements

Abstract
Statements correspond to the concept of action. For example, A: = 2 is an action which consists of assigning the value 2 to the variable A. WriteString (“GOOD BYE”) is another action: write the text GOOD BYE on the terminal.
Daniel Thalmann

6. The Input/Output Modules

Abstract
Suppose we wish to calculate the average and the sum of the square roots of the following numbers: 32.0, 58.0,102.0,9.0,135.0 and 93.0 The program shown in Fig. 6.1 will do the first part of the task (finding the average).
Daniel Thalmann

7. Enumeration and Subrange Types

Abstract
Five elementary types have been introduced so far: INTEGER, CARDINAL, REAL, BOOLEAN and CHAR. These types are predefined in the language MODULA-2 and they are called unstructured types, as opposed to the structured types which will be presented in Chapters 9,16 and 17.
Daniel Thalmann

8. Other Control Statements

Abstract
We now consider the scientific program of Fig. 8.1 which reads two experimental measures and performs certain computations on them. As it stands, this program could cause several problems for the user:
1)
If there is an odd number of data points, an error will occur.
 
2)
If m2 is less than mlim, the square root cannot be calculated.
 
3)
If VSqrt is equal to vlim, division is impossible.
 
Daniel Thalmann

9. Array Types

Abstract
Suppose we wish to read a word and then write it backwards, from right to left. This problem can be solved in a trivial way if the length of the word is known in advance. For example, if the length of the word is known to be six, the program in Fig. 9.1 will suffice. For example, if the word MODULA is read, the program will write ALUDOM.
Daniel Thalmann

10. Procedures and Locality

Abstract
We have indicated that a program may be viewed as a collection of statements which represent elementary actions. These actions may be sequential, repetitive or selective. A large program has a large number of statements, but it can generally be summarized in a much smaller number of more complex actions, or subproblems.
Daniel Thalmann

11. Transmission of Parameters and Scope

Abstract
In the discussion in the previous chapter, the parameters used were variable parameters. This means that the formal parameters must be preceded by the word VAR and the corresponding actual parameters must be variables.
Daniel Thalmann

12. Function Procedures and Procedure Types

Abstract
Procedures which give a unique result are often useful. For example, consider the following problems:
1)
Compute the sum of the elements of an array
 
2)
Find the largest number in a series.
 
3)
Compute Ae-x2 for specific values of X.
 
Daniel Thalmann

13. Recursion

Abstract
We explained in Section 11.3 that one consequence of the rules of scope is that a procedure may call itself. But what is the meaning of such a process? In fact, this is a familiar concept in mathematics, known as recursion.
Daniel Thalmann

14. Modules

Abstract
The module is the most important concept of MODULA-2 and gives the language its name.
Daniel Thalmann

15. Local Modules

Abstract
The modules studied so far have two parts: a definition section and an implementation section. They are also separately compilable, and we call them global modules. We have discussed the advantages of these modules and of their separate compilation. However, there are also drawbacks:
1)
These modules cannot be nested like procedures.
 
2)
The process is too complex when a programmer wants to implement a short module specific to his or her program.
 
Daniel Thalmann

16. Records

Abstract
As we have seen, arrays are collections of elements which are all of the same type. This is very convenient for many problems, but it is not well adapted to numerous cases. For example, a person is often characterized by his first name, his surname and his date of birth. Such a structure is typically hierarchial and may be represented as shown in Fig. 16.1.
Daniel Thalmann

17. Sets

Abstract
In mathematics, a set is typically defined as a collection of elements which share certain properties. For example, we may consider the set El of even prime numbers, or the set E2 of regular polyhedra in three dimensions.
Daniel Thalmann

18. Streams

Abstract
We have already seen that the basic MODULA-2 language does not provide any input-output operations. However, two standard modules InOut and ReallnOut have been widely used in the book to read data and write results. Up to now, we have always assumed that input was from the keyboard of the terminal and that results were displayed on the terminal screen or printed on paper. This was sufficient for all our examples. However, a computer generally has further peripheral devices. In particular, a computer has mass-storage media like discs to store large amounts of data.
Daniel Thalmann

19. Dynamic Data Structures

Abstract
In Section 16.4, we presented a module for handling several stacks. Although the definition module was well designed, it did have one important drawback: the maximum number of elements in a stack had to be defined. The reason for this was that the stacks were implemented using arrays. Typically, a stack differs from an array, because the number of elements in a stack is always varying. We say that an array is a static data structure, whereas a stack is a dynamic data structure. To simulate this dynamic structure using an array, we introduced an index (called Top) which indicated the element which was last entered on the stack. As illustrated in Fig. 19.1, if the maximum size of the array is 50, the only difference between a stack of 5 elements and a stack of 49 elements is the value of Top. The space required for 50 elements is reserved, even if it is not necessary.
Daniel Thalmann

20. Processes

Abstract
So far, we have emphasized the role of modules and procedures in decomposing a program into several parts. The most important advantage of such a strategy is that one part may be considered at a time without too much regard for the other parts.
Daniel Thalmann

21. Low-Level Features

Abstract
Programming languages are necessary in enabling communication between human beings and machines. When the first computers appeared in the fifties, these languages were very close to machine code, the so-called machine languages. Rapid development then led to programming languages which are much closer to natural languages. Moreover, these languages are formally defined and they emphasize the principles of structured programming. In particular, modern languages like MODULA-2 provide language constructions that dramatically reduce the number of potential errors in a program. As human beings are always considered to be more intelligent than machines, languages close to the machine are called low-level languages while human-oriented programming languages are called high-level languages.
Daniel Thalmann

Backmatter

Weitere Informationen