Previous Table of Contents Next


Listing 5.16. A demonstration of the definition and use of records.

  program fig16;

    type
      phonerecord = record
        firstname: string[15];
        lastname: string[10];
        midinit: char;
        areacode, exchange, number: integer;
        city: string[20];
      end;
 
    var
      phoneitem: phonerecord;
    begin
      { first method used to fill record variable }
      phoneitem.firstname := ‘Joe’;
      phoneitem.lastname := ‘Dialer’;
      phoneitem.midinit := ‘K’;
      phoneitem.areacode := 513;
      phoneitem.exchange := 232;
      phoneitem.number := 2323;
      phoneitem.city := ‘Jonestown’;

      { second method used to write record variables }
      with phoneitem do
        begin
          writeln(‘Firstname: ’, firstname);
          writeln(‘Lastname: ’, lastname);
          writeln(‘Middle Initial: ’, midinit);
          writeln(‘Phone Number: ’, areacode, ‘-’, exchange, ‘-’,
                   number);
          writeln(‘City: ’, city);
       end;
  end.

5.2.4.7. Sets and Set Algebra

It is possible to declare and work with algebraic sets and their operators by using the set of keyword in Turbo Pascal along with an ordinal identifier afterwards, much like an array is declared. Examples of sets of some degree have already been shown in Listing 5.8. case statements as well as if statements can take conditions of a number, character, or set being part of a set. Operators to test sets, as well as manipulate sets are described next. Examples of valid sets are shown in Figure 5.4, along with a listing of these operators and their effects. Note that sets are always enclosed in brackets, unless they are used as conditions in case statements or enumerated types.


Figure 5.4.  Examples of sets and set operators.

5.2.4.8. Enumerated Types

An enumerated type is a definition that can bring a degree of clarity to programs, if possible. These types can be specified in word terms, like a constant array definition, or set terms without the brackets. They are defined in the data type definition section, as records are, or in some cases can be placed in the var section. When an enumerated type is encountered, only values specified by the type can be placed into the variable. When stored in memory, an enumerated type’s label is assigned an ordinal number starting from 0 on the left-hand side. To help you fully understand, Listing 5.17 presents a short demonstration of the use of an enumerated type.

Listing 5.17. A demonstration of the use of an enumerated type.

  program fig17;
    type
      dirtype = (North, South, East, West);
    var
      i: dirtype;
      c: 1..5;
      { another enumerated type: May contain only 1, 2,3,4, or 5 }
    begin
  { actually in memory for i := 0 to 3 do, but this situation can
    tend to have more clarity as words than ordinal digits }
      for i := North to West do
        writeln(‘Hello’);
    end.

5.2.5. Procedures and Functions

This section contains descriptions of methods used to write procedures and functions in Turbo Pascal, as well as the proper way to use procedures and functions written as a result. It also describes the recursion capabilities of Turbo Pascal and contains a generic discussion of the kinds of procedures and functions that are prewritten and come with the Turbo Pascal compiler.

5.2.5.1. Procedures

Procedures are grouped sets of code; this code is executed when the procedure name is called. This is the Turbo Pascal equivalent of a subprogram or subprocedure. Examples of procedures that you have already encountered in Pascal are read, readln, write, and writeln. Procedures are used generally as a function of modularity, and in order to organize code when written.

When you’re writing procedures, you start with the keyword procedure and then an identifier, optionally with variable declarations inside parentheses as a parameters list. The variable declarations function as input and possibly output from these procedures.

The parameter declarations can be either of the constant form or the variable form when they are operated on within the procedure. When an identifier and a data type are listed, the variable used when calling the procedure is copied onto the stack. Any changes made to the variable are lost as the procedure ends. If the keyword var is placed before a data type group, then the original variables are passed by reference to the procedure, and they remain changed when the procedure ends.

Because procedures are similar to subprograms and can be written on their own as independent programs, constant declarations, data type statements, and variable declarations can be made within the procedure. These are considered local variables because their scope is locally defined to be the procedure itself, whereas variables defined in the main program would be considered global variables because they are globally defined for the entire program.

In addition, procedures must have data types in their data type declarations. These data types must fit the rules of variable identifiers, in that they cannot start with a number and cannot have a symbol that is used as part of the Turbo Pascal syntax. For example, arrays and defined-length Pascal string types must be redefined in the global data type definition list in order to pass them to procedures.

Examples of written procedures and procedure use are shown in Listing 5.18. Note that variable names used as local variables or procedure parameters may be the same as global variables or different. Variables at the same level must be identified uniquely, but variables at different levels may be identified using the same identifiers. The benefit of the organization of code may be seen in this listing. Also, as a function of modularity, determinants for more than one matrix could be found using the procedure, and not by recoding the determinant code.


Previous Table of Contents Next