Presentation is loading. Please wait.

Presentation is loading. Please wait.

James Tam Introduction To Defining New Types in Pascal In this section of notes you will learn about programmer-defined types.

Similar presentations


Presentation on theme: "James Tam Introduction To Defining New Types in Pascal In this section of notes you will learn about programmer-defined types."— Presentation transcript:

1 James Tam Introduction To Defining New Types in Pascal In this section of notes you will learn about programmer-defined types.

2 James Tam Declaring Types Why bother? Needed to pass arrays as parameters Creating your own type of variable Making a synonym for an existing type Syntax: Type Name(1) = Type for name (1); Name(2) = Type for name (2); : : : : Name(n) = Type for name (n);

3 James Tam Declaring Types (2) Example: type Board = array [1..8,1..8] of char; var chessBoard : Board; checkerBoard: Board;

4 James Tam Declaring Types (2) Example: Type Board = array [1..8,1..8] of char; Var chessBoard : Board; checkerBoard: Board; Declaring the type - defining what the type consists of Declaring variables of the new type

5 James Tam Declaring Types (3) Needed to pass arrays as parameters This won't work procedure proc (chessBoard : array of char); begin : end; A specific type needs to be passed in procedure proc (chessBoard : Board); begin : end; Not a type! Programmer-defined type Composite types like arrays cannot be returned from functions

6 James Tam Declaring Types (4) Can be used to provide alternative names for existing types Example: type FloatingPoint = real; var gpa : FloatingPoint; income: real;

7 James Tam Declaring Types (5) Can be used to provide alternative names for existing types Example: type FloatingPoint = real; var gpa : FloatingPoint; income: real; Original type still usable

8 James Tam Where Type Declarations Fit Into Pascal Programs program name; (* Declarations *) const (* Declaration of constants) type (* Declaration of new types *) var (* Declaration of variables *) (* Declarations of functions & procedures – defining what they do *) begin end.

9 James Tam Ordinal Types Enumerated types Subranges

10 James Tam Enumerated Types Syntax Type Name of type = (identifier-1, identifier-2..identifier-n); Example type Months = (January, February, March, April, May, June, July, August, September, October, November, December); Budget = array [January..December] of real; var tamjBudget : Budget; monthsIndex : Months; begin for monthsIndex := January to December do begin tamjBudget[monthsIndex] := 2000 + Random(2000); end; end.

11 James Tam Operations On Enumerated Types OperationName Equity= Inequity<> Less than< Less than, equal to<= Greater than> Greater than, equal to>= Predecessorpred Successorsucc Ordinal Numberord

12 James Tam Examples Of Operations On Enumerated Types Pred if ((pred(February)) = January) then writeln('Jan comes before Feb'); Ord writeln(ord(January)); (As an ASCII converter) ord('A'); ord (chr(65))

13 James Tam Subranges Used to define a new type which is a subset of an existing type. Syntax: type Subrange name = first value..last value; Example: type FallTerm = September..December; WinterTerm = January..April;

14 James Tam Summary You should now know how to define new, ordinal, types in Pascal programs.


Download ppt "James Tam Introduction To Defining New Types in Pascal In this section of notes you will learn about programmer-defined types."

Similar presentations


Ads by Google