Category : Modula II Source Code
Archive   : M2TUTOR.ZIP
Filename : CHAP02.TXT

Output of file : CHAP02.TXT contained in archive : M2TUTOR.ZIP

Chapter 2



We are ready to look at our first ================
instructional program in Modula-2. PUPPYDOG.MOD
Assuming that you have a full screen ================
editor of some type, load the program
PUPPYDOG.MOD and display it on your screen or examine a
printed listing of it. This is an example of the minimum
Modula-2 program. There is nothing that can be left out of
this program and still have a compilable, executable program.

The first word in the program, MODULE, is the name that
identifies a module, and it must be written as given here, in
all capital letters. During the first two parts of this
tutorial, we will only use this type of a module. There are
other types, but we will not look at any of them until we get
to part III of this tutorial. Modula-2 requires us to name
our module so we give it a name, PuppyDog in this case. We
could have used any name that qualifies as an identifier but
we have chosen a name that has nothing to do with computers
as an illustration that any name could be used. In a
practical program, you would probably use a name that was
descriptive of the program in some way.


An identifier is a combination of letters and numbers that
Modula-2 uses to identify a variable, program name, procedure
name, and several other quantities. In Modula-2, an
identifier is composed of any number of characters. The
characters may be any mix of alphabetic and numeric
characters, but the first character must be an alphabetic
character. The case of the alphabetic character is
significant such that PuppyDog, PUPPYDOG, and PuPpYdOg are all
different identifiers. No spaces or any other special
characters are allowed within an identifier.


The header line is terminated with a semicolon according to
the formal definition of Modula-2. A semicolon is a statement
separator and many semicolons will be used in large programs.
Following the semicolon, we come to the program itself. The
program statements are enclosed between the two words BEGIN
and END. In this case there are no statements, but if there


Chapter 2 - Getting Started in Modula-2

were some, they would be placed between the two indicated
words. Finally, the module name is repeated after the END and
it is followed by a period. The module name is repeated in
order to make the program easier to understand by clearly
marking its limits. In a program as small as this example,
it really doesn't add to the clarity, but in a large program,
it can be of significant help. The period marks the end of
the listing and can be thought of as similar to the period
that marks the end of a sentence.

The three words, MODULE, BEGIN, and END, are special words in
Modula-2. They are reserved words because they are used for
a specific purpose and cannot be used in any other way. They
are not available for your use in any way except for the
defined purpose. The reserved words in Modula-2 must be
capitalized or the compiler will not consider them as reserved
words. You will remember that alphabetic characters must have
the correct case in Modula-2. Some other languages, most
notably Pascal, allow you to use either case anywhere and it
converts them internally so that they are the same. It would
be permissible for you to use words such as Begin or End as
variables in a Modula-2 program because of the difference in
case, but it would be very poor programming practice and
should be avoided. We will come across many other reserved
words in these lessons. There are 40 reserved words in
Modula-2, and they are listed as follows;


We will study each of these words and how to use them in this

You should have learned how to use your compiler by now so you
can compile and run this program. It will do nothing, but
that is significant in itself, because it should at least
return to the operating system after it finishes doing
nothing. That may sound a little silly, but it does take a
considerable amount of effort to load, transfer control to the
program, and set up linkage back to your Disk Operating

It should be noted at this time that the Modula-2 compiler
doesn't care about extra blanks or linefeeds and the careful
programmer will insert extra blanks and linefeeds as desired
in order to make the program easier to read. As you continue


Chapter 2 - Getting Started in Modula-2

to program in Modula-2, you will no doubt develop a style of
your own and hopefully your programs can be read and
understood easily by other programmers.


Examine the program named WRITESM.MOD for ===============
an example of a Modula-2 program that does WRITESM.MOD
something. First you should notice that ===============
the elements of the first program are
still here as they will be in every Modula-2 program. The
same three reserved words are used here as before, but now
there are some added statements.

The third line begins with the reserved word FROM and is a
special line that must be used in any program that accesses
external procedures. We will not try to define line three at
this time. We will only say that every external call in
Modula-2 requires a definition of where to find the procedure.
The module named InOut is a Modula-2 collection of input and
output routines that are available for our use and this line
in the program tells the system to look in the InOut
collection for the procedures named WriteLn and WriteString.
When the program needs these particular subprograms to do what
we ask it to do, it knows where to find them. We will cover
the import list in detail later in this tutorial. Until then,
simply use the example programs as a guide when you wish to
write a practice program.


Between the begin and end statements in lines 5 and 19, which
we defined previously as the place where the actual program
is placed, we have a series of WriteString and WriteLn
statements. These statements are almost self explanatory,
but we will give a few words of explanation about them anyway.
Each line is a call to a procedure which is a very important
feature of Modula-2. A procedure is a servant that does a
specific job for us in a well defined way. In the case of the
WriteString, it looks at the string of characters supplied to
it and displays the string of characters on the monitor at the
current cursor position. The WriteLn procedure serves us by
moving the cursor down one line on the monitor and moving it
to the left side of the screen.

The parentheses are required for the WriteString procedure
because it has data following it. The data within the
parentheses is data supplied to our slave or helper. It gets
the string of characters between the quotation marks or the
apostrophes and displays the string on the monitor. You have


Chapter 2 - Getting Started in Modula-2

a choice of delimiters so that you can output the delimiters
themselves. If you desire to output a quotation mark to the
monitor, use apostrophes for delimiters, and if you wish to
output apostrophes, use quotation marks. If you wish to
output both, break the line up and output it piecemeal as in
lines 14 through 16.

This program should be very clear to you by now. First we
tell the system where to get the procedures, then we list the
procedures in the order required to produce the desired
results. It should be apparent that the lines of the program
between the reserved words BEGIN and END are simply executed
in order. Compile and execute the program and observe the
output on your monitor. As mentioned earlier, lines 26
through 29 give the result of executing the program, and your
compiler should give you the identical output. Lines 24
through 31 are within a comment area and will be ignored by
the compiler. Comments will be discussed in detail in the
next example program.

It should be mentioned at this point that it is possible to
direct the output to the printer or to a disk file but we will
not be doing that for quite some time. We will stay with the
basic syntax of Modula-2 for now.


No program is complete without a few ===============
comments embedded in the program as notes MODCOMS.MOD
to the programmer describing the reasons ===============
for doing some particular thing. The
notes are particularly helpful to another programmer who needs
to modify the program some day. It is not necessary for the
computer to understand the notes. In fact, you don't want the
computer to try to understand the notes, so you tell the
compiler to ignore the notes completely. How to do this is
the object of our next program named MODCOMS.MOD which you
should examine at this time.

In Modula-2, comments are enclosed in pairs of double
characters. The comment is started with the (*, and ended
with the *), and can extend over several lines if desired.
The program under consideration at this time, has several
examples of comments in it. If the comments were completely
removed, the program would be very similar to the last one but
a lot shorter. Notice that comments can go nearly anywhere
in a program, even before the header statement or after the
ending period. Comments can even be inserted within a
statement provided they go between tokens. (See programming
exercise 3 at the end of this chapter.)


Chapter 2 - Getting Started in Modula-2


Comments can be used to remove a section of program from
consideration by the compiler so that a particularly
troublesome section of code can be "commented out" until you
solve some of the other problems in program debugging. This
is illustrated in lines 18 and 19 which are ignored by the
compiler since they are within a comment area. It is
important to remember that comments can be nested in Modula-2
so that a section of code can be "commented out" even if it
contains other comments.

This particular program is not meant to be an example of good
commenting. It is really a sloppy looking program that would
need some work to put it into a good style, but it does
illustrate where it is possible to put comments. It should
be clear to you now that the result of execution section at
the end of each program is actually a comment as we mentioned
in the last section.


Examine the program named GOODFORM.MOD for ================
an example of a well formatted program. GOODFORM.MOD
Since Modula-2 allows you to use extra ================
spaces and blank lines freely, you should
use them in any way you desire to make your programs easy to
understand, and therefore easy to debug and modify. Special
care has been given to style in this program and it paid off
in a very easy to understand program. Even with your very
limited knowledge of Modula-2 programming, you can very
quickly decipher what it does. It is so well formatted that
comments are not needed and they would probably detract from
its readability. No further comment is needed or will be
given. Compile and run this program to see if it does what
you think it will do.


As you examine UGLYFORM.MOD, you are ================
seeing an excellent example of bad UGLYFORM.MOD
formatting. If you can see at a glance ================
what this program does, you deserve the
Nobel Prize for understanding software if such a thing exists.
The syntax for this program follows all of the rules of
Modula-2 programming except for good style. Without saying
anything else about this mess, I would suggest that you try
to compile and run it. You may be surprised to find that it
does compile and run, and in fact, it is identical to the last


Chapter 2 - Getting Started in Modula-2

program. Keep in mind that you can add extra blanks and
linefeeds anyplace you desire in a program to improve its

Hopefully, the last two programs will be an indication to you
that good programming style is important and can be a
tremendous aid in understanding what a program is intended to
do. You will develop your own programming style as time goes
by. It is good for you to spend some effort in making your
program look good, but don't get too excited about it yet.
Initially, you should expend your effort in learning how to
program in Modula-2 with reasonable style and strive to
improve your style as you go along. It would be good for now
if you simply tried to copy the style given in these lessons.


1. Write a program that will display your name on the

2. Write a program that will display your name on the
monitor along with your address, city, and state in 3
separate lines.

3. Add a comment to the MODCOMS.MOD file between the words
IMPORT and WriteLn to see if the compiler will allow you
to put a comment within a statement.


  3 Responses to “Category : Modula II Source Code
Archive   : M2TUTOR.ZIP
Filename : CHAP02.TXT

  1. Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!

  2. This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.

  3. But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: