Introduction INTRO TO MODULA-2 MODULA-2 IS A COMPLETE LANGUAGE ______________________________________________________________ Welcome to the programming language Modula-2, a very complete, high level language with many advanced features. Modula-2 was designed by Niklaus Wirth, the designer of Pascal. Based on experience with Pascal, Modula-2 was designed to make up for many of the deficiencies noted by programmers worldwide, and the changes make it a very powerful language with few limitations. In spite of its power, Modula-2 retains the simplicity of Pascal and can be used for small applications as easily as Pascal could be used. MODULA-2 TUTORIAL - PART I ______________________________________________________________ Even though there are many similarities between the two languages, the differences are significant. This tutorial was written considering both the similarities and the differences between the languages. The first part of this tutorial is composed of those features that are common to Pascal and Modula-2 and are also of a fundamental nature. You will need to study all of Part I in order to write meaningful Modula-2 programs. If you are already a fairly experienced Pascal programmer, you will absorb this material very rapidly. Be sure to go through it all at least once, because there are many small differences between the languages that you must consider. MODULA-2 TUTORIAL - PART II ______________________________________________________________ The topics taught in Part II of this tutorial are those advanced features that are also available in Pascal. Pointers, dynamic allocation, records, and linked lists are studied here along with other topics. These are very powerful tools that can be used to great advantage, but are quite often overlooked by many Pascal programmers that I have talked to. These are the tools that give Pascal and Modula-2 an advantage in flexibility over such languages as BASIC and FORTRAN. They do require a bit of deep concentration to fully understand, but you will be greatly rewarded if you take the time to understand and use them. I-1 Introduction to Modula-2 MODULA-2 TUTORIAL - PART III ______________________________________________________________ Part III of this tutorial covers those aspects of Modula-2 that are not included in Pascal in any way. Some of the topics are, independent compilation, the entire topic of modules, and concurrent processing. These are advanced topics and some of these topics may be the reasons that you selected Modula-2 as a programming language. The material covered in Part I in conjunction with that covered in Part III can lead to some very powerful programming techniques. To efficiently use this tutorial, you must carefully study all of the material in Part I, then you can do a lot of jumping around in Parts II and III and still cover the material in a meaningful manner. You may also choose to only study some chapters of the last two parts in order to learn the needed material for the programming problem at hand. It must be emphasized that it is important that you cover the material in Part I very carefully, and in order, since so much depends on what was taught before. When your study reaches the last two parts, comments at the beginning of each chapter will tell you which previous chapters must be understood in order to effectively use the material in the new chapter. FOR THE BEGINNING PROGRAMMER ______________________________________________________________ If you are a novice to computer programming, this course is for you because it is assumed that you know little or nothing about programming. Many sections, especially in the early chapters, will cover very basic topics for your benefit. Your biggest problem will be in setting up your compiler for use. This can be a very difficult task because Modula-2 is a complex language, and compiler writers must allow considerable flexibility in the use of the compiler. There are, at this time, a very limited number of Modula-2 compilers available, but it would not be possible to include notes on every compiler about how to install it for your computer. In addition, all compilers do not implement all functions defined in Niklaus Wirth's definition of the language. You may have a problem getting your compiler installed and running on your computer. The best help you can find is an experienced person in your company to help you get your compiler running properly on your computer. Modula-2, as defined by Niklaus Wirth, contains no provisions for input or output because they are so hardware dependent. It is up to each compiler writer to provide you with supplemental procedures for handling I/O (input/output) and a few other machine dependent features. Niklaus Wirth did recommend a library of I/O routines that should be available I-2 Introduction to Modula-2 and most compilers contain at least those facilities, and usually provide many more. SIMPLE EXAMPLES WILL BE USED ______________________________________________________________ All of the example programs are purposely kept simple and small to illustrate the point intended. It is of little value to you to present you with a large complex program to illustrate what can be illustrated in a small program better. In addition, every program is complete, and can be compiled and run. Program fragments frequently pose as many questions as they answer. The result of execution is given in comments at the end of each example program. The result is given for one compiler, but in nearly every case, the answer should be the same no matter what compiler is used. A few of the programs may output slightly different data because of different defaults. For example, there is no standard number of digits which will be output following a decimal point for a floating point number. The result of execution is given for your benefit so that you do not have to compile and run every program in those parts of the tutorial that you can breeze through quickly due to prior programming experience in a similar language. Because it would be a disservice to you to teach you a lot of simple techniques and never show you how they go together in a significant program, chapters 9 and 16 contain several larger example programs, all of which are complete programs. A relatively small amount of description is given about these programs because you will have already covered the details and should only need a quick overview of how to put the various constructs together. You will find some of these programs useful and you will have the ability to modify and enhance them for your use since you have the source code available on the distribution disk. SOME VARIABLE NAMES SEEM SILLY, WHY IS THAT? ______________________________________________________________ I have seen example programs with the same name everywhere, and had a hard time deciding what names were required and what could be changed to something more meaningful. For example a SORT program is in a file named SORT, the program name is SORT, the input data file is named SORT, and variables were named SORT1, SORT2, etc. This was no help to myself, a novice sorter, and would be no help to you. For that reason, the first program in chapter 2 is in a file named PUPPYDOG.MOD and the module name is PuppyDog. It should be obvious to even the newest programmer that the name of a module can be nearly anything if it is allowed to be PuppyDog. You will learn I-3 Introduction to Modula-2 later that well selected names can be a great aid in understanding a program. This will be evident in some of the early programs when variable names are chosen to indicate what type of variable they are. Some compilers require that the module name be the same as the file name, and all require them to agree when you get to global modules because of the way type checking is accomplished. It would be best for you to get into the habit of naming them both the same now. For that reason, all of the example programs use the same name for the file name and for the module name. Your compiler may allow you to use different names. It will be left up to you to study your manual and see what the requirements are for your particular compiler. WHAT IS A COMPILER? ______________________________________________________________ There are two primary methods used in running any computer program that is written in a readable form of English. The first method is through use of an interpreter. An interpreter is a program that looks at each line of the english program, decides what the english on that line means, and does what it says to do. If one of the lines is executed repeatedly, it must be scanned and analyzed each time, greatly slowing down the solution of the problem at hand. A compiler, on the other hand, is a program that looks at each statement one time and converts it into a code that the computer understands directly. When the compiled program is actually run, the computer does not have to figure out what each statement means, it is already in a form the computer can run directly, which results in a much faster execution of the program. Due to the nature of Modula-2, there will be few, if any, interpreters for Modula-2. WHAT ABOUT THE PROGRAMMING EXERCISES? ______________________________________________________________ The programming exercises at the end of each chapter are a very important part of the tutorial. If you do them, you will embed the principles taught in each chapter more firmly in your mind than if you ignore them. If you choose to ignore them, you will be somewhat adept at reading Modula-2 programs but very ineffectual at writing them. By doing the exercises, you will also gain considerable experience in using your editor and compiler. I-4 Introduction to Modula-2 THE ANSWERS DIRECTORY ______________________________________________________________ The ANSWERS directory on the distribution disk contains an answer to each of the programming exercises suggested at the end of each chapter. Note carefully that the given answer is only one possible solution to the problem and your answer could differ markedly and still be a correct answer. The filename for each answer is given in the form of CHnnEm.MOD, where nn refers to the chapter number and m refers to the exercise number for that chapter. If multiple answers are requested, they will be named with an additional a, or b following the primary name. Thus the filename CH04E3A.MOD contains the answer to the first part of programming exercise 3 in chapter 4. It will be assumed that you know how to use your compiler and that you have some kind of an editor for use with the example files. With the above in mind, you are now ready to begin your tour of Modula-2. A sample program is included in this chapter for you to try with your compiler. It is left as an exercise for you to compile and run FIRSTEX.MOD. When you can successfully compile and run this program, you are ready to begin the tutorial on Modula-2 programming. At this point, don't worry about what the statements mean in FIRSTEX.MOD, you will have a complete understanding of this program by the time you complete chapter 4 of this tutorial. I-5