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 >
Wrap
Text File
|
1993-06-12
|
7KB
|
180 lines
Variable Types and Declarations.
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
Variable declarations must indicate the Type, which circumscribes the set
of values it can have and the operations that can be performed on it.
All variables must be declared before they can be assigned.
Simple Types - Ordinal Types (counting numbers 0,1,2,3 etc.)
──────────── or Real Types (with fractional parts such as 4.0 or 1.23)
Ordinal Types - predefined - integer, shortint, longint, byte, word,
───────────── boolean and char.
- user-defined - enumerated and subrange types.
Shortint is a signed 8-bit integer, in the range -128..127
Integer is a signed 16-bit integer, in the range -32768..32767
Longint is a signed 32-bit integer, in the range -2147483648..2147483647
Byte is an unsigned 8-bit integer, in the range 0..255
Word is an unsigned 16-bit integer, in the range 0..65535
Boolean values are denoted by the predefined constant identifiers 'false'
and 'true' and are of the enumerated type. Thus:
false<true Ord(false) = 0 Ord(true) = 1
Char types have a set of values ordered according to the extended ASCII
character set. (A = 65.......Z = 90.....a = 97......z = 122). Appendix B
of the Programmer's Guide.
Enumerated types define ordered sets of values as in the example
type
suit = (club,diamond,heart,spade);
The first enumerated constant in a list has the ordinality of zero.
i.e Ord(club) is zero, Ord(diamond) is 1, etc.
A Subrange type is a range of values from an ordinal type called the 'host
type' as in the examples: 0..99 -128..127 club..heart.
It has the properties of the host but the run-time value must be in the
specified range.
Real Types. - real, single, double, extended, comp
─────────── (6 4 8 10 8 size in bytes.
These types differ in the range and precision of the values with the
extended type ranging from 3.4 * 10^-4932.. 1.1 * 10^4932 and having
19-20 significant digits. See pages 26 - 27 of the Programmer's Guide
for more detail.
String Types.
─────────────
A type string value is a sequence of characters with dynamic length
attribute ( = actual character count at execution) and a constant size
attribute from 1 to 255. The standard function 'length' returns the
attribute's current value.
The character (ch) at any position (n) in a string (str) is obtained by
the assignment ch := str[n];
The order of string values is determined by the order of the character
values in corresponding positions, starting from the leftmost character.
Structured Types. - array, set, file or record types.
─────────────────
Array Types.
────────────
Arrays have a fixed number of components of one type, defined as in the
examples:
array[1..100] of real; array[1..10,1..8] of boolean;
Set Types.
──────────
Although in mathematics there are no restrictions on the objects which may
be members of a set, in Pascal the members must all be of the same type.
Examples are:
Type
DaysOfMonth = set of 0..31;
Letter = set of 'A'..'Z';
Colours = set of (Red,Green,Blue);
File Types.
───────────
A file type consists of a linear sequence of components of the component
type. The number of components is not set by the declaration, but is
extended dynamically as required. The example program RECORDS.PAS shows a
use of file types, with
SalesFile : File of SaleType;
where SaleType is a record, as defined below.
The standard file type 'text' signifies a file containing characters
organised into lines, which are not limited in number.
Record Types.
─────────────
A record type comprises a set number of components, or fields, that can be
of different types. The record type declaration specifies the type of each
field and the identifier that names the field, as in the following example:
Type
SaleType = Record
Name : string[50];
Item : string[20];
Quantity : integer;
UnitPrice : real;
VAT : real;
end;
Pointer Types.
──────────────
A pointer type defines a set of values that point to dynamic variables of a
specified type called a base type. A pointer type variable contains the
memory address of a dynamic variable. The program POINTERS.PAS contains a
pointer type variable as shown below:
Type
PersonPointer = ^PersonRecord;
{ read this as 'PersonPointer is a pointer to PersonRecord' }
PersonRecord = record
Name : string[50];
Job : string[50];
Next : PersonPointer; { Pointer to next record }
end;
In this example, a variable of type PersonPointer contains the address of
a dynamic variable of type Personrecord.
The main advantage of using pointers is that the variable pointed to by a
pointer is allocated on the Heap and not on the Data Segment, which is
limited to a size of 64k. The Heap can grow in size to fill all the
remaining memory if required.
The allocation of memory on the Heap is achieved by using the procedure New
as in the example program POINTERS.PAS:
New(NewPerson);
where NewPerson is an variable of type PersonPointer declared in the VAR
declaration part of the program.
Heap memory is de-allocated by using the procedure Dispose, or the
procedures Mark and Release. See the notes on Pointers for further details.
Global and local variables.
───────────────────────────
Global variables are declared at the beginning of the program and are
within the 'scope' of the whole program, whereas local variables are
declared within a procedure or function and the scope is limited to that
procedure or function.
Although it might be confusing to the reader of the program, it is possible
to use the same variable, say 'i', as both a global and local variable,
without confusion between the two, as long as they are declared both
globally and locally within the procedure or function. Failure to declare
in both places will cause erroneous results however.
VARIABLE.TXT
18.4.90