Programs written in the Modula-2 notation are generally compiled. (See section 1.8.3.) That is, the programmer cannot simply write out the instructions, type run and get immediate results on the console. Rather, one uses an editing program to create a text version of the program and then compiles this into a code version that the machine can execute.
In a typical implementation of Modula-2 (and of many other notations) the writing and execution of a program involves the following steps:
1. Carefully define the problem to be solved. (The discussion of Chapter 1 will be followed up on in more detail throughout the text.)
2. Refine the plan into tasks and sub-tasks as necessary
3. Write out the solution to the problem using a high level notation such as Modula-2.
4. Enter a text version of the program into the computer using an editing program and save it for later reference.
5. Run the compiler program to translate the text version to a machine-readable form.
6. Link the file(s) created at step two with others previously compiled (or supplied with the system) to generate a program that will actually run by itself.
7. Run the program.
8. Test the program and redevelop as necessary.
Figure 2.1 shows a developer's-eye view of a computing system being used for problem solving:
In some cases, direct action by the programmer is not required to achieve linking. Linking may be performed automatically at run time (and therefore be invisible to the programmer.) On the other hand, it may require that a separate program be run using the compiled file(s) as input and an executable file as output.
In this book, little emphasis will be placed on the compiling, linking, and interpreting processes. Instead, the focus will be on writing efficient, error-free programs that will compile and run smoothly. The details of how to use particular computers, their operating systems, editors, compilers, and linkers, are beyond the scope of this book. Such instruction must be obtained from separate manuals supplied with the specific system, or provided in an instructional lab.
Of the major functions of a computer (input, storage, processing, control, and output), only the processing step lies within the definition of a program given in the introduction. Storage usually is handled more or less automatically by the system, though we shall later see that the programmer also can control this to a limited extent. Even input and output are, in some senses, peripheral to the task of processing, despite the fact that without them the computer would have no means of obtaining its data or of delivering any results. Consider yourself: If you cannot articulate your opinions (output) you may as well not have any. So, what good is a box that calculates but does not give the answers to the outside world?
Perhaps more important is the fact that input and output are handled differently on various computers. That is, these are hardware-dependent functions. When the routines that handle these operations are entirely contained within the computing notation itself, programs written for one computer are unlikely to work on another one. For these reasons, the input and output functions have been separated from the Modula-2 language proper. They must be imported into the programs from another part of the system called the library. The contents of this library always look the same to the programs that employ it (This is what standards are for), but the code that implements the library will be quite different on different machines. This importation is illustrated in the first sample program, shown below.
MODULE HiThere; FROM STextIO IMPORT WriteString; BEGIN WriteString ("Hello Master, how may I serve you?"); END HiThere.
The output from this program is the line:
Hello Master, how may I serve you?
NOTE: The place from which WriteString is usually obtained is called STextIO or InOut