home *** CD-ROM | disk | FTP | other *** search
/ C!T ROM 2 / ctrom_ii_b.zip / ctrom_ii_b / PROGRAM / PASCAL / PASTUT34 / VARIABLE.TXT < prev    next >
Text File  |  1993-06-12  |  7KB  |  180 lines

  1.                       Variable Types and Declarations.
  2.                       ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
  3.  
  4.  Variable declarations must indicate the Type, which circumscribes the set
  5.  of values it can have and the operations that can be performed on it.
  6.  
  7.  All variables must be declared before they can be assigned.
  8.  
  9.  Simple Types  -  Ordinal Types  (counting numbers 0,1,2,3 etc.)  
  10.  ────────────     or Real Types  (with fractional parts such as 4.0 or 1.23)
  11.  
  12.  Ordinal Types  -  predefined  -  integer, shortint, longint, byte, word,
  13.  ─────────────                    boolean and char.
  14.  
  15.                 -  user-defined -  enumerated and subrange types.
  16.  
  17.  
  18.  Shortint is a signed 8-bit integer, in the range -128..127
  19.  Integer is a signed 16-bit integer, in the range -32768..32767
  20.  Longint is a signed 32-bit integer, in the range -2147483648..2147483647
  21.  Byte is an unsigned 8-bit integer, in the range    0..255
  22.  Word is an unsigned 16-bit integer, in the range   0..65535
  23.  
  24.  Boolean values are denoted by the predefined constant identifiers 'false'
  25.  and 'true' and are of the enumerated type. Thus:
  26.  
  27.  false<true    Ord(false) = 0    Ord(true) = 1
  28.  
  29.  Char types have a set of values ordered according to the extended ASCII
  30.  character set. (A = 65.......Z = 90.....a = 97......z = 122). Appendix B
  31.  of the Programmer's Guide.
  32.  
  33.  Enumerated types define ordered sets of values as in the example
  34.  
  35.  type
  36.      suit = (club,diamond,heart,spade);
  37.  
  38.  The first enumerated constant in a list has the ordinality of zero.
  39.  i.e Ord(club) is zero, Ord(diamond) is 1, etc.
  40.  
  41.  A Subrange type is a range of values from an ordinal type called the 'host
  42.  type' as in the examples:  0..99      -128..127        club..heart.
  43.  It has the properties of the host but the run-time value must be in the
  44.  specified range.
  45.  
  46.  
  47.  Real Types.  - real, single, double, extended, comp  
  48.  ───────────    (6      4       8        10      8       size in bytes.
  49.  
  50.  These types differ in the range and precision of the values with the
  51.  extended type ranging from 3.4 * 10^-4932.. 1.1 * 10^4932 and having
  52.  19-20 significant digits.  See pages 26 - 27 of the Programmer's Guide
  53.  for more detail.
  54.  
  55.  
  56.  String Types.
  57.  ─────────────
  58.  
  59.  A type string value is a sequence of characters with dynamic length
  60.  attribute ( = actual  character count at execution) and a constant size
  61.  attribute from 1 to 255. The standard function 'length' returns the
  62.  attribute's current value.  
  63.                                                                           
  64.  The character (ch) at any position (n) in a string (str) is obtained by
  65.  the assignment ch := str[n];
  66.  
  67.  The order of string values is determined by the order of the character
  68.  values in corresponding positions, starting from the leftmost character.
  69.  
  70.  
  71.  Structured Types.  -  array, set, file or record types.
  72.  ─────────────────
  73.  
  74.  Array Types.
  75.  ────────────
  76.  
  77.  Arrays have a fixed number of components of one type, defined as in the
  78.  examples:  
  79.           array[1..100] of real;      array[1..10,1..8] of boolean;
  80.  
  81.  Set Types.
  82.  ──────────
  83.  
  84.  Although in mathematics there are no restrictions on the objects which may
  85.  be members of a set, in Pascal the members must all be of the same type.
  86.  Examples are:
  87.               Type  
  88.                  DaysOfMonth = set of 0..31;  
  89.                  Letter = set of 'A'..'Z';  
  90.                  Colours = set of (Red,Green,Blue);
  91.  
  92.  File Types.
  93.  ───────────
  94.  
  95.  A file type consists of a linear sequence of components of the component
  96.  type.  The number of components is not set by the declaration, but is
  97.  extended dynamically as required. The example program RECORDS.PAS shows a
  98.  use of file types, with      
  99.                          SalesFile : File of SaleType; 
  100.  
  101.  where SaleType is a record, as defined below.
  102.  
  103.  The standard file type 'text' signifies a file containing characters
  104.  organised into lines, which are not limited in number.
  105.  
  106.  
  107.  Record Types.
  108.  ─────────────
  109.  
  110.  A record type comprises a set number of components, or fields, that can be
  111.  of different types. The record type declaration specifies the type of each
  112.  field and the identifier that names the field, as in the following example:
  113.  
  114.  Type 
  115.     SaleType = Record 
  116.       Name       : string[50]; 
  117.       Item       : string[20]; 
  118.       Quantity   : integer; 
  119.       UnitPrice  : real; 
  120.       VAT        : real; 
  121.     end; 
  122.  
  123.  
  124.  Pointer Types.
  125.  ──────────────
  126.  
  127.  A pointer type defines a set of values that point to dynamic variables of a
  128.  specified type called a base type.  A pointer type variable contains the
  129.  memory address of a dynamic variable.  The program POINTERS.PAS contains a
  130.  pointer type variable as shown below:
  131.  
  132.     Type  
  133.       PersonPointer = ^PersonRecord;  
  134.         { read this as 'PersonPointer is a pointer to PersonRecord' }
  135.   
  136.       PersonRecord = record  
  137.             Name : string[50]; 
  138.             Job  : string[50]; 
  139.             Next : PersonPointer;        { Pointer to next record } 
  140.          end;
  141.  
  142.  In this example, a variable of type PersonPointer contains the address of
  143.  a dynamic variable of type Personrecord. 
  144.  
  145.  The main advantage of using pointers is that the variable pointed to by a
  146.  pointer is allocated on the Heap and not on the Data Segment, which is
  147.  limited to a size of 64k.  The Heap can grow in size to fill all the
  148.  remaining memory if required. 
  149.  
  150.  The allocation of memory on the Heap is achieved by using the procedure New
  151.  as in the example program POINTERS.PAS: 
  152.  
  153.      New(NewPerson); 
  154.  
  155.  where NewPerson is an variable of type PersonPointer declared in the VAR
  156.  declaration part of the program. 
  157.  
  158.  Heap memory is de-allocated by using the procedure Dispose, or the
  159.  procedures Mark and Release. See the notes on Pointers for further details.
  160.  
  161.                              
  162.  Global and local variables. 
  163.  ───────────────────────────
  164.  
  165.  Global variables are declared at the beginning of the program and are
  166.  within the 'scope' of the whole program, whereas local variables are
  167.  declared within a procedure or function and the scope is limited to that
  168.  procedure or function. 
  169.  
  170.  Although it might be confusing to the reader of the program, it is possible
  171.  to use the same variable, say 'i', as both a global and local variable,
  172.  without confusion between the two, as long as they are declared both
  173.  globally and locally within the procedure or function.  Failure to declare
  174.  in both places will cause erroneous results however. 
  175.  
  176.  
  177.  VARIABLE.TXT
  178.  18.4.90
  179.  
  180.