Presentation is loading. Please wait.

Presentation is loading. Please wait.

Pascal Programming Pascal Units, Abstract Data, Ordinals, Arrays.

Similar presentations


Presentation on theme: "Pascal Programming Pascal Units, Abstract Data, Ordinals, Arrays."— Presentation transcript:

1 Pascal Programming Pascal Units, Abstract Data, Ordinals, Arrays

2 Pascal Programming n The Pascal Unit... n The unit is a compiled procedure or function that can be saved and reused. n You compile the element to disk rather than compile to memory. n You set the compiler, from the pull-down menu, to Destination Disk. n Your stored program now has a.exe—it’s executable. n Now it will run from the DOS environment without Turbo running.

3 Pascal Programming n Unit—a collection of procedures, functions, defined constants and some other items that can be compiled apart from the program. n Procedures and functions have declarations just as they would in a program. n In a unit...the interface section contains the headings for all of the procedures or functions. Comments should be included. n The balance of each procedure or function is put in the implementation section. n A unit begins with the reserved word unit.

4 Pascal Programming n Syntax –Unit name; Interface Implementation –end. n Usually the unit, when filed, should have the same name as the coded unit before being saved to disk. The file will have a.tpu extension. n You will wanted to save the coded unit before compiling to disk. n uses (name); after the program heading will call the unit. Multiple units may be listed. n Units can use units the same way.

5 Pascal Programming n Predefined units are found in the TURBO.TPL file so that the program has access to them at all times. n You can add your units to this directory by following the help directions... Or n Use a defined directory for your units MyUnits. Then, pull-down options and click directories then activate Unit directories.

6 Pascal Programming n Turbo Pascal comes with predefined units– crt, clrscr. So, uses crt; and clrscr; after begin will clear the screen. n Everything declared in the interface section will be public. It is not in the implementation section. n Private declarations are local. Thus, a program may reuse the identifier elsewhere. n Details can be hidden in the implementation section.

7 Pascal programming n Synonyms: private variables...owned variables, static variables, closed variables. These variable may be used by all within the unit. n Unlike a local variable, and owned variable holds its value. If called again, that will be the initial value. n Units should be initialized to avoid carryover values.

8 Pascal Programming n An abstract data type – integer –has been predefined. The means it uses to fulfill the operators that affect it are hidden. n A programmer can declare and define a data type with incidental details hidden. n The process of hiding the incidental details is called data abstraction.

9 Pascal programming n New types are defined within a unit... –Type Type_name = type definition; e g: Type –Scores = integer; –Averages = real –Won_lost = char;

10 Pascal Programming n The order of declarations... n Constants n Type n Variables n Procedures & Functions.

11 Pascal programming n Ordinal Types n A type with values that can be listed is called Ordinal Type. n An ordinal type’s value comes from its position in the list. So, an operator like < can be used to test its value. n Two ordinals produce a Boolean –1<3 and a<d

12 Pascal Programming n The ordering of char follow the ASCII order (Appendix 11 in the text.) n Testing two ordinals can be useful when alphabetizing a list or files. n The operations of, = all work. n for loops naturally use ordinal type. The initial/final values must be of the same type as the variable. n e g: –for letter := ‘a’ to ‘z’ do write (Letter)

13 Pascal Programming n Predefined functions deliver the previous (pred) and successive (succ) values. n ord will identify a value in a list—ord (9) returns 8 because the integer lists starts with 0.

14 Pascal Programming n Subrange Types n Subrange is a sub-set of an ordinal list. n A subrange type declaration always consists of two constant values separated by two dots. –e g: type –SmallInteger = -10.. 10 n These two value are chosen from a host type. So, values of the subrange type have the same relationship as the host type. n A subrange can disclose an error that falls outside the expected range.

15 Pascal Programming n Enumerated Types n An enumerated type is a list of identifiers. –e g: type –Awards =(bronze, silver, gold); n Type declarations can be mixed without an order.


Download ppt "Pascal Programming Pascal Units, Abstract Data, Ordinals, Arrays."

Similar presentations


Ads by Google