home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / modula2 / mod2txt.zip / CHAP3.TXT < prev    next >
Text File  |  1987-03-25  |  30KB  |  652 lines

  1.                    Chapter 3 - The simple Modula-2 data types
  2.  
  3.  
  4.              The material in this chapter is extremely important  to
  5.         you as you strive to become a good Modula-2 programmer,  but
  6.         you  may  also  find it to be somewhat  tedious  because  it
  7.         contains so many facts.  This material is needed in order to
  8.         develop the topics in the next few chapters,  but all of the
  9.         details are not necessarily required.   For that reason, you
  10.         may wish to go through it rather rapidly picking up the high
  11.         points  and come back to this chapter for the details  later
  12.         when  they will be much more meaningful.   Do not completely
  13.         pass  over  this  material  at this time  or  the  next  few
  14.         chapters  will be meaningless unless you are already  highly
  15.         experienced in other programming languages.
  16.  
  17.                           A PROGRAM WITH VARIABLES
  18.  
  19.              Load  and display the program named INTVAR.MOD for  our
  20.         first  program  with  some variables in  it.   This  program
  21.         begins in the usual way since it has a MODULE header and the
  22.         IMPORT  list.   Next we come to a new  reserved  word,  VAR.
  23.         This  word is used to indicate to the compiler that we  wish
  24.         to  define one or more variables.   In Modula-2,  there is a
  25.         rule that says you can use nothing until it is defined.   If
  26.         we  wish  to use a variable in the program,  we  must  first
  27.         define  that it will exist,  and what kind of a variable  it
  28.         is.  After  that,  it can be used in the program to do  what
  29.         needs to be done.
  30.  
  31.              Following the reserved word VAR,  we have the  variable
  32.         named "Count" defined.   The reserved word INTEGER following
  33.         the  colon states that the variable "Count" will be of  type
  34.         INTEGER.  This  means  that it can store  any  whole  number
  35.         between  -32768 to 32767.   Don't worry too much about  this
  36.         yet, the next program will completely define what an INTEGER
  37.         type  variable is.   It is important to recognize that after
  38.         we have defined the variable "Count",  it still doesn't have
  39.         a value stored in it, that will come later.
  40.  
  41.              The  next line has two more variables  defined,  namely
  42.         "x",  and "y".   They are also INTEGER type variables and do
  43.         not  have a value stored in them yet.   You can think of the
  44.         three  variables  as  three empty  boxes,  each  capable  of
  45.         storing a number but with no number in them yet.   It  would
  46.         be  perfectly permissible to put all three variables on  one
  47.         line,  or  to  have separated them such that each was  on  a
  48.         separate line.  At this point, the program doesn't know that
  49.         there  is any difference between them,  because there  isn't
  50.         any.   The  fact that one will contain the sum of the  other
  51.         two  has no meaning yet,  the comments are only for us,  not
  52.         the computer.
  53.  
  54.  
  55.  
  56.  
  57.                                    Page 13
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.                    Chapter 3 - The simple Modula-2 data types
  68.  
  69.  
  70.                         USING VARIABLES IN A PROGRAM
  71.  
  72.              Now  we will go to the program itself.   The first line
  73.         sets  the variable "x" equal to 12,  in effect  putting  the
  74.         number 12 in the box mentioned earlier.   The sign := is the
  75.         Modula-2  symbol for assignment.   It is most meaningful  to
  76.         read  the symbol "gets the value of" since it is not  really
  77.         stating  a  mathematical equality but is saying  in  effect,
  78.         "assign the value of this to the variable at the left."  The
  79.         entire  line can be read as "x gets the value of 12."  There
  80.         is now a value assigned to the variable "x" declared in  the
  81.         header.   The  next statement assigns the value of 13 to the
  82.         variable "y".   Finally the value of the data stored in  the
  83.         variable "x" is added to the value of the data stored in the
  84.         variable "y", and the sum is stored in the variable "Count".
  85.         We  have  therefore done our first calculations in  Modula-2
  86.         but we will do many more before this tutorial is completed.
  87.  
  88.              Notice  that  each  statement  is  terminated  with   a
  89.         semicolon,  a Modula-2 requirement.
  90.  
  91.              The  three variables are then displayed on the  monitor
  92.         with  appropriate  prose to identify  them.   The  only  new
  93.         statement  here  is  the "WriteInt" procedure that  needs  a
  94.         little  explanation.  This  procedure is used to  output  an
  95.         INTEGER  type variable to the monitor or whatever device  is
  96.         being  used.   By  definition,  it contains  two  quantities
  97.         within the parentheses,  the variable name and the number of
  98.         columns it should fill.   If there are not enough columns to
  99.         output the data, more will be used so that no digits will be
  100.         truncated.   If all are not needed,  leading blanks will  be
  101.         output.   If  the variable "x" had the value of 1234 when we
  102.         came to program line 18,  all four digits would be output in
  103.         spite of the request for three.  Since "x" has the value  of
  104.         12, only two columns will be used and one leading blank will
  105.         be  output.   In like manner,  "y" is allotted 4 columns and
  106.         "Count" is to be output in 6 columns.
  107.  
  108.              The last two lines of the program assign new values  to
  109.         two  of  the variables.   The variable "x" is  assigned  the
  110.         value  of  FF hexadecimal which is 256 decimal,  and "y"  is
  111.         assigned the value of 177 octal which is 127 decimal.   This
  112.         is only done as an illustration to you of how it is done. If
  113.         you  don't understand these two  numbering  systems,  simply
  114.         ignore this until you have a need for it.
  115.  
  116.              Compile and run this program to see if it does what you
  117.         expect  it  to do.   The important thing to notice  in  this
  118.         program is the variable definition in the definition part of
  119.         the module and the variable assignment in the program  part.
  120.         It  should be obvious,  but it would be well to mention that
  121.  
  122.  
  123.                                    Page 14
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.                    Chapter 3 - The simple Modula-2 data types
  134.  
  135.  
  136.         the  definition part of the module extends from  the  module
  137.         name   to  the  reserved  word  "BEGIN"  and  is  where  all
  138.         definitions  are put.   Likewise,  the program part  of  the
  139.         module  includes  all  statements from the  "BEGIN"  to  the
  140.         "END".
  141.  
  142.                            SIMPLE VARIABLE TYPES
  143.  
  144.              Modula-2 has several predefined data types that you can
  145.         use  in your programs.   You also have the ability to define
  146.         any  number of complex types built up from the simple  types
  147.         but  we will not discuss this until we get to chapter 6  and
  148.         beyond.   The  simple types  are  INTEGER,  CARDINAL,  REAL,
  149.         BOOLEAN,  and  CHAR.   Each has its own purpose and its  own
  150.         peculiarities and we will cover each type one at a time.
  151.  
  152.                        THE SIMPLE VARIABLE - INTEGER
  153.  
  154.              Load  and display the program named INTMATH.MOD for  an
  155.         example  of  INTEGER math.   In the declaration part of  the
  156.         program (the part prior to the BEGIN) we have 7 INTEGER type
  157.         variables defined for use in the program.   We will use them
  158.         to illustrate INTEGER arithmetic.
  159.  
  160.             An INTEGER variable, by definition,  can store any whole
  161.         number  between -32768 and 32767.   An attempt to store  any
  162.         other  value in an INTEGER type variable should  produce  an
  163.         error by your compiler but it may produce some other result.
  164.         Some  compilers may store a -32769,  which is one count  too
  165.         low,  as a 32767 which is at the top end of the range.  This
  166.         is  due  to the two's complement arithmetic that  you  don't
  167.         need to understand at this point.  It will be left to you to
  168.         determine what your compiler does in such a case.
  169.  
  170.              The first line in the program is nothing new to you, it
  171.         simply assigns the variable "A" the value of 9.   The second
  172.         line adds 4 to the value stored in the variable "A" and  the
  173.         result,  13, is stored in the variable "B".  Next the values
  174.         stored  in the variables "A" and "B" are added together  and
  175.         the  sum,  which  is  9  + 13,  is stored  in  the  variable
  176.         "IntSum".  Continuing in the same manner, the difference and
  177.         the  product  are calculated and stored.   When we  come  to
  178.         INTEGER  division,  we are breaking new ground  because  the
  179.         result  is  truncated to the largest whole number  resulting
  180.         from the division.   Thus 13 DIV 9 results in 1 because  the
  181.         remainder  is simply dropped.   The next construct,  B MOD A
  182.         results  in the remainder of the division,  in this case  4.
  183.         You  will find these operations very useful as you  progress
  184.         as a Modula-2 programmer.
  185.  
  186.  
  187.  
  188.  
  189.                                    Page 15
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.                    Chapter 3 - The simple Modula-2 data types
  200.  
  201.  
  202.              The  intent of the next line is to illustrate that  you
  203.         can  do  several math operations in a statement if  you  are
  204.         careful to put the parentheses in the proper places.   There
  205.         are definite rules about operator precedence but I recommend
  206.         that  you use lots of parentheses to remove all doubt as  to
  207.         what the results will be.
  208.  
  209.              The  results  of  the operations  are  displayed  in  6
  210.         columns and we move on to several new operations.  The first
  211.         new  operation is the "INC" which is short for  "increment".
  212.         This  simply  increments the variable contained  within  the
  213.         parentheses and if a second argument is given,  the variable
  214.         is incremented by the value of that variable. In like manner
  215.         the   "DEC"  procedure  decrements  the  variable   in   the
  216.         parentheses  by  one  unless a second argument is  given  in
  217.         which case the variable is decremented by the value of  that
  218.         variable.
  219.  
  220.              It  may  not  be clear at this point,  but  the  second
  221.         variable  itself  may  be another variable name  or  even  a
  222.         composite  of  several as long as it results in  an  INTEGER
  223.         type variable.  This is illustrated in the program.
  224.  
  225.              Finally,  we  come  to the last two procedures in  this
  226.         program, the "MIN" and the "MAX".  These two procedures will
  227.         return  the value of the smallest possible  INTEGER,  -32768
  228.         and  the largest possible INTEGER,  32767.   These  are  the
  229.         values returned for a 16 bit microcomputer which is what you
  230.         are  probably  using  since that is what  this  tutorial  is
  231.         intended for.  It would be well to add that not all Modula-2
  232.         compilers  implement  these  functions so you  may  need  to
  233.         comment out these two lines in order to compile and run this
  234.         program.
  235.  
  236.              Compile  and run this program and observe  the  output.
  237.         If  your  compiler results in errors,  you may have to  make
  238.         some  changes  in  order  to  compile  it.    Refer  to  the
  239.         COMPILER.DOC file on the distribution disk for notes on some
  240.         of the more popular Modula-2 compilers.
  241.  
  242.                        THE SIMPLE VARIABLE - CARDINAL
  243.  
  244.              Load  and  display the file named CARDMATH.MOD  for  an
  245.         example of CARDINAL mathematics and output.  In this file, 7
  246.         variables are defined as CARDINAL and one more as INTEGER. A
  247.         CARDINAL variable can store any whole number from 0 to 65535
  248.         in  a  16  bit  microcomputer,  although the  range  may  be
  249.         different if you are using an unusual computer or compiler.
  250.  
  251.              The first few lines are the same as the last program so
  252.         very  little  needs  to be said about them  except  for  the
  253.  
  254.  
  255.                                    Page 16
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.                    Chapter 3 - The simple Modula-2 data types
  266.  
  267.  
  268.         subtraction  example.   In  this  case,  the result  of  the
  269.         subtraction  would be negative if it were carried out as  in
  270.         the  last program so "A" is subtracted from "B".   It is  an
  271.         error  to attempt to store a negative number in  a  CARDINAL
  272.         type  variable.   For that reason,  a CARDINAL should not be
  273.         used  if there is any chance that it will be required to  go
  274.         negative.   Programming experience will be the best  teacher
  275.         when  it  comes  to deciding what variables to use  in  each
  276.         situation.
  277.  
  278.              In this program the variables are once again displayed,
  279.         but  now the procedure named "WriteCard" is used for  output
  280.         because the variables to output are CARDINAL.
  281.  
  282.              The  next  two  statements indicate  that  INTEGER  and
  283.         CARDINAL variables are "assignment compatible" meaning  that
  284.         they  can  be assigned to each other with the  :=  operator.
  285.         They cannot however, be mixed in calculations.  Constants in
  286.         an  expression  are  assumed to be of the same type  as  the
  287.         variables in the expression and they must agree.   For  that
  288.         reason,  the expression in line 36 is invalid because (-112)
  289.         is required to be a CARDINAL constant but it is negative and
  290.         therefore not CARDINAL.  In the prior line it is permissible
  291.         to  subtract 112 from the value of "A" as long as the result
  292.         is still positive.  As an exercise, change line 34 such that
  293.         a number less than 112 is assigned to "A".  The program will
  294.         compile without error but when you run it,  you should get a
  295.         runtime  error  because the CARDINAL assignment  is  out  of
  296.         range.   Notice  that the constant value of -112 is allright
  297.         for use an an INTEGER variable.
  298.  
  299.              The remaining statements in the program are the same as
  300.         the last program so additional explanation is unneeded.   It
  301.         would be good to point out that in the case of CARDINAL, the
  302.         "MIN"  and  "MAX"  procedures will return values  of  0  and
  303.         65535 for most 16 bit implementations.
  304.  
  305.              Compile and run this program remembering that it may be
  306.         necessary  to comment out the "MIN" and "MAX" statements  to
  307.         get a successful compilation.
  308.  
  309.                          THE SIMPLE VARIABLE - REAL
  310.  
  311.              Load  and display the program named REALMATH.MOD for  a
  312.         demonstration of the data type REAL.  The definition part of
  313.         this  program is similar to the last with some additions  to
  314.         the IMPORT list.   Your compiler may use different names for
  315.         some of the procedures here,  so if you get a compile  error
  316.         you  will  need to modify these.   We will study the  IMPORT
  317.         (and EXPORT) list in detail later, so be patient.
  318.  
  319.  
  320.  
  321.                                    Page 17
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.                    Chapter 3 - The simple Modula-2 data types
  332.  
  333.  
  334.              Several  REAL variables and one each of the INTEGER and
  335.         CARDINAL types are defined for use in the program.  The REAL
  336.         type  variable can contain numbers in a wide range and  with
  337.         fractional  parts  included.    The  exact  range,  and  the
  338.         accuracy will vary widely depending on your  implementation.
  339.         It  will be up to you to check your reference manual for the
  340.         limits on your computer and compiler.  A REAL type number is
  341.         defined  as one with a decimal point.   The mathematics  are
  342.         the  same  as with the other two except  that  the  division
  343.         symbol  is the slash (/).   There is no "MOD" for REAL  type
  344.         numbers because there is theoretically no remainder, since a
  345.         fractional part is computed as part of the calculation.
  346.  
  347.              The  four results are displayed on the monitor with  12
  348.         columns  allowed  for  each  result  and  two  extra  blanks
  349.         displayed  between each number.   Unfortunately,  we have no
  350.         control over how many digits will be displayed following the
  351.         decimal point.   This would be nice for outputting data in a
  352.         financial  model  where  we would like to  have  two  digits
  353.         following  the decimal point.   When we get to the  advanced
  354.         part  of this tutorial,  we will write our own procedure for
  355.         doing  that  in  such a way that we can  call  it  from  any
  356.         program just like we call these output procedures.
  357.  
  358.                        CONVERSION BETWEEN DATA TYPES
  359.  
  360.              Beginning  in  line  37,  we  assign  the  INTEGER  and
  361.         CARDINAL  variables  some values and convert the  values  to
  362.         type REAL by using the procedure "FLOAT".   We then  convert
  363.         the  variable  "Sum" to INTEGER and CARDINAL by use  of  the
  364.         procedure "TRUNC".  The fractional part, if any, will simply
  365.         be  thrown away.   These procedures will be very  useful  in
  366.         many of your programs.
  367.  
  368.              The  last  two  lines return the value of  the  largest
  369.         possible  REAL number and the smallest REAL number for  your
  370.         implementation.   Once again,  your compiler may not support
  371.         these two functions and they may have to be commented out in
  372.         order to compile.
  373.  
  374.              Compile and run this program.
  375.  
  376.                        THE SIMPLE VARIABLE - BOOLEAN
  377.  
  378.              Load  and display the file named BOOLMATH.MOD  on  your
  379.         monitor  for  an example of BOOLEAN  variables.   A  BOOLEAN
  380.         variable  can only have one of two possible values,  TRUE or
  381.         FALSE.   These variables cannot be printed directly but  can
  382.         be  used  to control other print statements to print  out  a
  383.         representation of their value.  We will see how later.
  384.  
  385.  
  386.  
  387.                                    Page 18
  388.  
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.                    Chapter 3 - The simple Modula-2 data types
  398.  
  399.  
  400.              We  define 3 BOOLEAN variables and 3 INTEGER  variables
  401.         and  assign values to the 3 INTEGER variables in the program
  402.         for  use  in these illustrations.   In line 13  the  BOOLEAN
  403.         expression "A = 22" is TRUE,  therefore the BOOLEAN variable
  404.         "IsIt"  is  assigned the value TRUE.   The  variable  "IsIt"
  405.         could be used later in the program to make a decision,  by a
  406.         yet undefined method, to do something or bypass it.  In like
  407.         manner,  the  next statement assigns "IsIt" the value  FALSE
  408.         because A is not equal to 23.   The remainder of the allowed
  409.         BOOLEAN  expressions are defined in the next few  lines  and
  410.         are left for your inspection and study.
  411.  
  412.              Beginning in line 25, composite BOOLEAN expressions are
  413.         illustrated.   As many BOOLEAN expressions as desired can be
  414.         combined with AND and OR operators.   If two or more BOOLEAN
  415.         expressions are combined with the AND, the result is TRUE if
  416.         all   expressions  are  TRUE.    If  two  or  more   BOOLEAN
  417.         expressions are combined with the OR,  the result is true if
  418.         any of them are TRUE.  The NOT operator inverts the sense of
  419.         what it modifies,  it turns a TRUE to FALSE and  vice-versa.
  420.         Finally a couple of composite BOOLEAN expressions are  given
  421.         for  illustration  of  the  amount  of  complexity  that  is
  422.         allowed,  although there is no real limit as to how far  you
  423.         can go with the complexity.  Good programming practice would
  424.         dictate that you keep it simple and understandable.
  425.  
  426.                       TWO RULES FOR BOOLEAN EVALUATION
  427.  
  428.              First  it  is important that you use the same  type  of
  429.         variables  within  a  BOOLEAN  expression.   REAL's  can  be
  430.         compared  to  REAL's and INTEGER's to INTEGERs,  but  REAL's
  431.         cannot  be compared to INTEGER's.   CARDINAL and CHAR  types
  432.         can  also  be compared to their own types,  but none of  the
  433.         four can be compared directly to each other.
  434.  
  435.              Secondly, Modula-2 uses a shortcut evaluation technique
  436.         for  BOOLEAN evaluation.   Evaluation proceeds from left  to
  437.         right  and  if  it  finds a  result  which  will  positively
  438.         determine the outcome, evaluation stops.  For example, if it
  439.         is evaluating a string of 5 comparisons all combined with an
  440.         AND,  and it finds that the second term is FALSE, evaluation
  441.         stops there.  Since all terms must be TRUE for the result to
  442.         be TRUE,  it makes no difference what values the last  three
  443.         are, the result will be FALSE because of the second term.
  444.  
  445.                          THE SIMPLE VARIABLE - CHAR
  446.  
  447.              Load  and display the program named CHARDEMO.MOD for an
  448.         illustration of the last simple variable type,  CHAR.   Text
  449.         data is stored in a computer in a format utilizing the  CHAR
  450.         data type.  Although there are exceptions, such as when text
  451.  
  452.  
  453.                                    Page 19
  454.  
  455.  
  456.  
  457.  
  458.  
  459.  
  460.  
  461.  
  462.  
  463.                    Chapter 3 - The simple Modula-2 data types
  464.  
  465.  
  466.         is  stored  in some form of a packed mode,  this  is  nearly
  467.         always  true.    This  tutorial  was  written  with  a  word
  468.         processor  that uses a CHAR type for text storage,  and  few
  469.         word processors use any other method.
  470.  
  471.              Although  there are many different ways to store  text,
  472.         only  two are used to any level of significance,  EBCDIC and
  473.         ASCII.  ASCII is used almost exclusively in micro computers.
  474.         I have never heard of an implementation that used EBCDIC  in
  475.         a  microcomputer,  so we will limit our discussion to ASCII.
  476.         This merely refers to the way the characters of the alphabet
  477.         and  all other characters are represented in  the  computer.
  478.         The  ASCII standard defines that the value of 65 will be the
  479.         letter 'A',  66 will be the letter 'B',  etc.   If  everyone
  480.         uses  the same standard,  transfer of data from one computer
  481.         to another is greatly simplified.
  482.  
  483.              The program named CHARDEMO has the usual header with  4
  484.         CHAR  type  variables defined for use in  the  program.   An
  485.         INTEGER is also defined.  In the program itself, we begin by
  486.         assigning  2 of the variables some CHAR data.   Since a CHAR
  487.         variable  is  capable of storing  one  letter,  numeral,  or
  488.         special  character,  each variable is assigned  one  letter.
  489.         The single or double quotes are used as an indication to the
  490.         compiler  that you intend for it to use the letter as a CHAR
  491.         type  variable  rather than as another  variable  name.   Of
  492.         course  if  you wanted to use "A" as a  variable  name,  you
  493.         would  have  to  define  it in the definition  part  of  the
  494.         module.
  495.  
  496.                         TWO SPECIAL CHAR PROCEDURES
  497.  
  498.              The  next  instruction gets the ordinal  value  of  the
  499.         letter  "A",  adds two to it,  and assigns that value to the
  500.         variable  "Index",  which  must be an INTEGER  (although  it
  501.         could  have  been  defined as a  CARDINAL).   Refer  to  the
  502.         documentation that came with your computer and you will find
  503.         an  ASCII  table  that will define the  letter  "A"  as  65.
  504.         Finally,  the  CHAR  type  variable "Dog3" is  assigned  the
  505.         character value of "Index".  Your ASCII table should  define
  506.         67  as the letter "C".   It is important to understand  that
  507.         the    CHAR   variable   "Dog3"   contains   the   character
  508.         representation of the letter "C",  and the INTEGER  variable
  509.         "Index"   contains   the  numerical  value  of   the   ASCII
  510.         representation  of  the letter "C".   It would be  perfectly
  511.         allright  to  use  the  variable  "Index"  for  any  desired
  512.         numerical calculations,  but not to display the letter  "C".
  513.         On the other hand,  it would be allright to use the variable
  514.         "Dog3" to display the letter "C" on the monitor but it could
  515.         not be used for any calculations.  The purpose therefore, of
  516.         the two procedures "ORD" and "CHR", is to translate from one
  517.  
  518.  
  519.                                    Page 20
  520.  
  521.  
  522.  
  523.  
  524.  
  525.  
  526.  
  527.  
  528.  
  529.                    Chapter 3 - The simple Modula-2 data types
  530.  
  531.  
  532.         type to the other.
  533.  
  534.              The  variable  "Cat4" is assigned the double  quote  by
  535.         enclosing  it in the single quotes,  and the characters  are
  536.         output in a funny order to spell "CAT.  The variable "Char1"
  537.         is  assigned  the  value "S",  and  the  word  is  completed
  538.         resulting  in the full word "CATS" on the monitor after  the
  539.         program  is compiled and run.
  540.  
  541.             If this were the only way to use the CHAR type variable,
  542.         it  would  be very tedious and frustrating,  but  there  are
  543.         other  methods to use the CHAR type that are far more useful
  544.         as you will see.
  545.  
  546.              Next,  an  additional  means of assigning a  CHAR  type
  547.         variable is given.  By assigning the CHAR variable "65C", it
  548.         is  the same as writing CHR(65),  resulting in the  variable
  549.         having  the  internal  value "A".   A number less  than  256
  550.         followed  by  a "C" is defined by Modula-2 as  a  CHAR  type
  551.         constant.
  552.  
  553.              Finally,  the  variable "Char1" is assigned the  letter
  554.         "a" and it is converted to upper case "A" with the procedure
  555.         CAP.   This procedure will convert the argument to its upper
  556.         case  equivalent  if  it is a lower  case  letter.   If  its
  557.         argument is an upper case letter or any other character,  it
  558.         will do nothing to it.
  559.  
  560.              Compile and run this program and observe the output.
  561.  
  562.                           USING THE TRANSFER PROCEDURES
  563.  
  564.              Load  and  display  the  file  named  TRANSFER.MOD  for
  565.         several  examples of transferring data between  the  various
  566.         simple  data types.   The transfer functions given here  may
  567.         not  seem  too important at this time,  but some time  spent
  568.         here will help to reduce the frustration later when you  get
  569.         seemingly  wrong errors that say you are using  incompatible
  570.         types  in  a  statement.   All of the program  will  not  be
  571.         discussed,  only  those statements that use some of the more
  572.         unusual capabilities of Modula-2.
  573.  
  574.              In  line  13,  the  calculations are  done  in  INTEGER
  575.         format,  but  due to the assignment compatibility of INTEGER
  576.         and CARDINAL, the result is converted to CARDINAL across the
  577.         ":=".  Line 16 is an illustration of mixed mathematics using
  578.         the  transfer  procedure  INTEGER.   Line 20  is  the  first
  579.         example  of "nested" procedures which must be  done  because
  580.         FLOAT only uses a CARDINAL for an argument.
  581.  
  582.  
  583.  
  584.  
  585.                                    Page 21
  586.  
  587.  
  588.  
  589.  
  590.  
  591.  
  592.  
  593.  
  594.  
  595.                    Chapter 3 - The simple Modula-2 data types
  596.  
  597.  
  598.              The  expression  in line 22 is an error  because  TRUNC
  599.         results  in a CARDINAL which cannot be added to an  INTEGER.
  600.         Either  of the next two lines fix the problem by making  the
  601.         addition  type-compatible then making use of the  assignment
  602.         compatibility  between INTEGER and CARDINAL for line  number
  603.         23.   The same error occurs in line 26 and is fixed the same
  604.         way in either of the next two lines.   Once again,  in  line
  605.         31,  the  incompatible  type  error occurs and is  fixed  in
  606.         either of two ways in the next two lines.
  607.  
  608.              Lines  35  and  36 illustrate converting CHAR  data  to
  609.         first  CARDINAL  then REAL which requires  nested  procedure
  610.         calls.  The last line of the program is a nest of procedures
  611.         which  converts a character from CHAR to CARDINAL,  then  to
  612.         REAL,  back  to CARDINAL,  and finally back to the  original
  613.         CHAR  variable.   It  does  nothing except  act  as  a  good
  614.         illustration to you of what can be done.
  615.  
  616.              Conversion  between types is very important.   You will
  617.         use  these techniques often so it is important to  know  how
  618.         they  work.   A  very  simple yet helpful memory aid  is  to
  619.         remember  that any simple type can be converted to  CARDINAL
  620.         and  CARDINAL  can be converted to  any  type.   Most  other
  621.         conversions require two steps to get from one to the other.
  622.  
  623.              Chapter  14  will readdress this topic with  even  more
  624.         extensive type transfer procedures.
  625.  
  626.         PROGRAMMING PROBLEMS
  627.  
  628.         1.   Write  a program in which you define,  using the CHAR
  629.              type variable, the letters "a" and "z", and the numbers
  630.              "0" and "9".  Convert them to CARDINAL, and display the
  631.              four characters and their ASCII (numerical) values.
  632.  
  633.         2.   Write a program that you can easily modify to experiment
  634.              with conversions between the various types that result in
  635.              incorrect conversions to see the results on your compiler.
  636.              For example, convert a -1 as an INTEGER to a CARDINAL.
  637.  
  638.  
  639.  
  640.  
  641.  
  642.  
  643.  
  644.  
  645.  
  646.  
  647.  
  648.  
  649.  
  650.  
  651.                                    Page 22
  652.