Gunther Blaschek Gustav Pomberger Introduction to Programming with Modula -2 With 26 Figures Springer-Verlag Berlin Heidelberg New York London Paris Tokyo Hong Kong Barcelona Authors Gunther Blaschek Gustav Pomberger Institut fUr Wirtschaftsinformatik und Organisationsforschung lohannes-Kepler-Universitat Linz AltenbergstraJ3e 69, A-4040 Linz-Auhof, Austria Translator Robert Bach 180 Seyler Drive Petersburg, VA 28305, USA CR Subject Classification (1987): D.3, D.2.1-2, 0.2.5 ISBN-13: 978-3-540-52038-2 e-ISBN-13: 978-3-642-75322-0 DOl: 10.1007/978-3-642-75322-0 Library of Congress Cataloging-in-Publication Data Blaschek, Giinther, 1957- [EinfUhrung in die Programmierung mit Modula-2. English] Indroduction to programming with Modula-2 / Giinther Blaschek, Gustav Pomberger; [translator, Robert Bach]. p. cm. Translation of: EinfUhrung in die Programmierung mit Modula-2. Includes bibliographical references (p. ) and index. 1. Modula-2 (Computer program language) I. Pomberger, Gustav, 1949- . II. Title QA76.73.M63B5813 1990 005.13'3-dc20 90-10220 CIP This work is subject to copyright. All rights are reserved, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in other ways, and storage in data banks. Duplication on this publication or parts thereofis only permitted under the provisions of the German Copyright Law of September 9, 1965, in its current version, and a copyright fee must always be paid. Violations fall under the prosecution act of the German Copyright Law. © Springer-Verlag Berlin Heidelberg 1990 The use of registered names, trademarks, etc. in this publication does not imply, even in the absence of a specific statement, that such names are exempt from the relevant protective laws and regulations and therefore free for general use. 2145/3140-543210 - Printed on acid-free paper Preface This book is intended for the novice as well as for the experienced programmer who wants to learn Modula-2. We do not limit ourselves to just a description of Modula-2. Instead, we seek to familiarize the reader with the concept of algorithms and to show him/her how to implement algorithms in Modula-2. The programming language Modula-2 was developed by Niklaus Wirth (also the father of world-famous Pascal) and made public in 1978. Compared to other programming languages such as Ada, COBOL or PL/!, Modula-2 is a compact language, which makes it easy to learn. Nevertheless, Modula-2 contains all important language elements necessary for formulating complicated algorithms and for implementing the modern concepts of software engineering. Modula-2 is distinguished by a systematic structure that makes it possible to write easily readable programs. The language supports many of the principles of modern software engineering. All this makes Modula-2 a useful instrument for an introduction to the basics of programming. This textbook strives to establish a solid foundation in the techniques of programming with up-to-date methods of program development. Use of the programming language Modula-2 is reinforced with numerous hands-on exercises. This book does not presuppose any knowledge of programming, but it does require a certain ability in the realm of abstract thinking, some pleasure in problem solving, and a desire to come to terms with complex interrelationships. The book consists of two parts, each divided into three chapters. One part treats the basic concepts of programming (Chapters 1,2 and 5); the other deals with programming in Modula-2 (Chapters 3, 4 and 6). Chapter 1 introduces the term "algorithm" and attempts to broaden the reader's understanding of various types of problems and their algorithmic solutions. This chapter is intended primarily for novices. The basic concepts of program development are treated in Chapter 2. We show how difficult and intractable problems can be solved by systematic reduction to constituent subproblems. We further present the criteria by which the resulting sub tasks can be combined as modules to solve the original task. This chapter likewise addresses the VI Preface novice. The program development strategies discussed here are, however, by no means trivial and are foreign to many experienced programmers, who might also profit from this section. Chapter 3 describes the programming language Modula-2 progressively from simple to complex elements. It is organized in such a way that it serves as a reference for the programmer, i.e., the chapter contains all details relevant to the Modula-2 programmer. With the basics covered in the first three chapters, specialized problems in the application of Modula-2 can now be discussed in Chapter 4. The problem of program testing and a discussion of what comprises good programming style are the subjects of Chapter 5. Even experienced programmers should take this chapter to heart, although they could argue that it still treats basics. Good programming style can by no means be taken for granted out in the field. The area of hardware has produced breathtaking advances in recent years. The development of the principles of software engineering was somewhat less rapid but no less important. Chapter 6 discusses how Modula-2 supports the concepts of software engineering. Most of the chapters and their sections contain exercises intended to help the reader to enrich his/her knowledge and to help monitor progress. Above all, the collective experience of the authors in teaching such courses as Introduction to Programming, Algorithms and Data Structures, Compilers, Software Engineering, and various programming projects was channelled into this book. Most of the examples and exercises were taken from preparations for these courses. Acknowledgements It is impossible to list all the people who influenced the publication of this book. We are particularly indebted to Prof. N. Wirth of Zurich; his work in the area of programming languages-above all the definitions of Pascal, Modula-2 and Oberon-have had an immense impact on computer science and have greatly influenced our work. We owe special thanks to Prof. P. Rechenberg for valuable suggestions and discussion as well as for reading the manuscript. We are also indebted to our colleagues P. Mossenback and P. Schnorf, who meticulously studied the manuscript and suggested valuable improvements. We also want to thank our publisher for the fine cooperation. Finally, we wish to express special thanks to R. Bach for his diligent English translation that included updating our German version. Linz, Austria G. Blaschek March 1990 G. Pomberger Table of Contents 1 Algorithms ................................................................................ . 1.1 Examples of Algorithms .............................................................. 1 1.2 Definition of the Tenn "Algorithm" ................................................ 5 1.3 Objects and Actions .................................................................. 8 1.4 Means of Representing Algorithms ................................................ 15 1.5 Some Simple Algorithms . .... ..... .... ............ ... ... ..... ... .... ... ... ... ... .... 22 1.6 Algorithms and Programs ............................................................ 28 Exercises .. ... .. . .. . .. . ... .. . .. . .. . .. .... .. . .. .... ... .. . .. .... .. . ..... ... ... . ... ... ... ... .. . . 30 2 Principles of Program Development ............................................. 33 2.1 The Principle of Stepwise Refinement ............................................. 33 2.2 Data Capsules ... ..... ...... .... ..... .... ........ ... ...... ...... ... ... ... ....... ....... 41 2.3 The Module Concept ................................................................. 45 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 3 The Programming Language Modula-2 .......................................... 50 3.1 Notation for Language Description .. .... ... ............ ............ ... ..... ... ..... 50 3.2 Lexical Elements of Modula-2 ...................................................... 52 3.2.1 Identifiers ..................................................................... 52 3.2.2 Key Words .................................................................... 53 3.2.3 Numbers ...................................................................... 54 3.2.4 Character Strings ............................................................ 56 3.2.5 Operators and Delimiters .. .... ..... ... .... ... ......... ...... ..... ........... 58 3.2.6 Separation of Symbols . ... .... ........ ... .... ... ..... ....... ...... ... ... .... 59 3.2.7 Comments ..................................................................... 59 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 3.3 Elementary Program Structure ....... .... ... ..... .... ... ..... ... .... ... ... .......... 61 3.4 Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 3.4.1 Data Types . ... ...... .... ..... ...... ......... .... ............... ... ... ...... .... 64 3.4.1.1 Predefined Data Types ............................................ 64 3.4.1.2 Elementary User-Defined Data Types ........................... 69 3.4.1.3 Structured Data Types ............................................ 70 3.4.2 Declaration of Constants .................................................... 77 vrn Table of Contents 3.4.3 Declaration of Types ......................................................... 78 3.4.4 Declaration of Variables .... ... ....... ..... ... ....... ........ ... ...... ... ..... 79 3.4.5 Type Identity Among Objects..... ... ..... ....... ........... ... ...... ... ..... 80 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 3.5 Expressions ........................................................................... 82 3.5.1 Classes of Expressions . ... ...... . .. ... .... ..... ...... ... . ... ..... ....... .. . . 82 3.5.2 Operands ...................................................................... 82 3.5.3 Operators ...................................................................... 84 3.5.4 Rules for Writing Expressions ............................................. 85 3.5.5 Expression Compatibility of Operands .................................... 89 Exercises .. .. . .. ... . ... .. . .. ... .... ... .. . .. .... ... .. ... .... ... .. . .. . ... .. ... . ... .. . ... . 89 3.6 Statements .............................................................................. 90 3.6.1 Assignment .................................................................... 92 3.6.2 Conditional Statements ...................................................... 93 3.6.2.1 The IF Statement ................................................... 93 3.6.2.2 The CASE Statement ............................................. 95 Exercises .. .. ... . .. . .. . .. ... . .. .... ... .. ... ....... .. ... ....... .. . .. . ..... ... . ... ... ... . 98 3.6.3 Loops ........................................................................... 98 3.6.3.1 The WHILE Statement ............................................ 99 3.6.3.2 The REPEAT Statement .......................................... 100 3.6.3.3 The FOR Statement ................................................ 102 3.6.3.4 The LOOP and EXIT Statements ................................. 104 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . .. . . . . . . . . . . .. . . . . . . . . . . .. . . . . . . . . . . . . .. 106 3.6.4 The WITH Statement ......................................................... 106 Exercises .............................................................................. 108 3.7 Procedures ............................................................................. 109 3.7.1 Declaration of Procedures ................................................... 109 3.7.2 Invocation of Procedures .................................................... 112 3.7.3 The RETURN Statement .................................................... 114 3.7.4 Function Procedures ......................................................... 116 3.7.5 Scope and Lifetime of Objects in Procedures ............................. 118 3.7.6 Data Exchange Between Procedures ....................................... 120 3.7. 7 ARRAY Parameters ........................................ '" .............. 123 3.7.8 Standard Procedures ......................................................... 124 3.7.9 PROCEDURE Types and Procedure Variables ........................... 129 Exercises ....................................... , ...................................... 133 3.8 The Module Concept of Modula-2 .................................................. 134 3.8.1 Local Modules ............................................................... 134 3.8.2 Scope and Lifetime of Objects in Modules ................................ 138 3.8.3 Modules and Separate Compilation ........................................ 139 3.8.3.1 Definition Modules ................................................ 140 3.8.3.2 Implementation Modules .......................................... 143 Exercises .............................................................................. 145 3.9 System-Dependent Language Properties .......................................... 145 3.9.1 TheModuleSYSTEM ...................................................... 145 3.9.2 Type Transfer Functions ................................................... 150 3.9.3 Absolute Addressing of Variables ......................................... 152 Table of Contents IX 3.10 Processes and Coroutines ............................................................ 153 3.10.1 Creation and Synchronization of Processes ............................... 154 3.10.2InterruptsandPriorities ...................................................... 159 4 Writing Modula-2 Programs ......................................................... 163 4.1 Some Simple Modula-2 Programs .................................................. 163 4.2 Input/Output ........................................................................... 169 4.2.1 The Module Terminal ......................................................... 171 4.2.2 The Modules InOut and RealInOut ......................................... 172 4.2.3 The Module FileSystem ...................................................... 177 4.3 Dynamic Data Structures ............................................................. 181 4.3.1 Working with Dynamic Data Structures ................................... 182 4.3.2 Dynamically Created RECORDs with Variants ........................... 187 4.4 Recursion .............................................................................. 188 4.4.1 Recursive Procedures ........................................................ 189 4.4.2 Inner Structure of Recursive Procedures and How They Work ......... 191 4.4.3 Some Examples of Recursive Algorithms ................................. 192 4.4.4 Advantages and Disadvantages of Recursive Programming ............ 195 4.5 Language Extension Modules ....................................................... 195 4.5.1 The Module MathLibO ....................................................... 196 4.5.2 The Module String ............................................................ 197 4.5.3 The Module Storage ......................................................... 198 4.6 Parallel Processes ..................................................................... 199 Exercises ..................................................................................... 209 5 Programming Style and Program Testing ....................................... 211 5.1 ProgrammingStyle ................................................................... 211 5.1.1 Structure ....................................................................... 212 5.1.2 Naming Conventions ......................................................... 217 5.1.3 Commentary .................................................................. 217 5.1.4 Outward Form of Programs ................................................ 218 5.2 Program Testing ...................................................................... 219 5.2.1 Types of Error ................................................................ 219 5.2.2 The Testing Procedure ....................................................... 220 5.2.3 Built-In Testing and Debugging Aids ...................................... 221 5.2.4 Debuggers ..................................................................... 223 6 Software Engineering with Modula-2 ............................................ 224 6.1 Modula-2 as a Tool for Specification ............................................... 224 6.2 Modularizing and Structuring ....................................................... 225 6.3 Data Capsules and Data Abstraction ................................................ 226 6.4 Separate Compilation and Type Binding ........................................... 230 x Table of Contents Appendix A: Syntax Diagrams ......................... ............................ 234 Appendix B: Predefined Identifiers .............................................. 247 Appendix C: Compatibility Rules ................................................. 248 Appendix D: ASCII Table ............................................................ 252 References .................................................................................... 253 Index ........................................................................................... 254 1 Algorithms The goal of this book is to convey the knowledge needed to make effective use of the programming language Modula-2. Before leaming how to use a programming language, a student first needs to know what a program is and how programs are designed. Therefore, this chapter introduces some of the most important basics of programming. Since these are fundamentals, we cannot simply define them with the help of other concepts. Instead, we will describe them on the basis of examples. Algorithms and programs are closely related. Computer science has no monopoly on algorithms. We encounter algorithms in everyday life without recognizing them as such. In computer science, however, they playa very central role. That is why we need to come to terms with what is meant by the term "algorithm," what attributes algorithms possess, and what meaning they have for programming. We will first present several examples of algorithms, then point out the properties of algorithms in general, and then arrive at a definition of the term algorithm. After describing various methods of representing algorithms, we give typical examples of simple algorithms and, in conclusion, discuss the connection between algorithms and programs. 1.1 Examples of Algorithms In order to solve a problem, we need an unambiguous set of instructions that determines exactly which actions are to be executed and in which order. The formal description of such an approach to a solution is known as an algorithm. Here is a simple example: (1) Add two natural numbers, a and b. (2) Assign the sum to the variable s. An algorithm consists of a sequence of more or less complicated actions. Each action requires the presence of objects upon which it is to act and whose change in state represents the result of the action. The objects in the above algorithm, for example, are a, band s.