160 likes | 463 Views
Pascal Programming. Pascal Units, Abstract Data, Ordinals, Arrays. Pascal Programming. The Pascal Unit . . . The unit is a compiled procedure or function that can be saved and reused . You compile the element to disk rather than compile to memory.
E N D
Pascal Programming Pascal Units, Abstract Data, Ordinals, Arrays
Pascal Programming • The Pascal Unit . . . • The unit is a compiled procedure or function that can be saved and reused. • You compile the element to disk rather than compile to memory. • You set the compiler, from the pull-down menu, to Destination Disk. • Your stored program now has a .exe—it’s executable. • Now it will run from the DOS environment without Turbo running.
Pascal Programming • Unit—a collection of procedures, functions, defined constants and some other items that can be compiled apart from the program. • Procedures and functions have declarations just as they would in a program. • In a unit . . .the interface section contains the headings for all of the procedures or functions. Comments should be included. • The balance of each procedure or function is put in the implementation section. • A unit begins with the reserved word unit.
Pascal Programming • Syntax • Unit name; • Interface • Implementation • end. • 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. • You will wanted to save the coded unit before compiling to disk. • uses (name); after the program heading will call the unit. Multiple units may be listed. • Units can use units the same way.
Pascal Programming • Predefined units are found in the TURBO.TPL file so that the program has access to them at all times. • You can add your units to this directory by following the help directions . . . Or • Use a defined directory for your units MyUnits. Then, pull-down options and click directories then activate Unit directories.
Pascal Programming • Turbo Pascal comes with predefined units– crt, clrscr. So, uses crt; and clrscr; after begin will clear the screen. • Everything declared in the interface section will be public. It is not in the implementation section. • Private declarations are local. Thus, a program may reuse the identifier elsewhere. • Details can be hidden in the implementation section.
Pascal programming • Synonyms: private variables . . .owned variables, static variables, closed variables. These variable may be used by all within the unit. • Unlike a local variable, and owned variable holds its value. If called again, that will be the initial value. • Units should be initialized to avoid carryover values.
Pascal Programming • An abstract data type – integer –has been predefined. The means it uses to fulfill the operators that affect it are hidden. • A programmer can declare and define a data type with incidental details hidden. • The process of hiding the incidental details is called data abstraction.
Pascal programming • New types are defined within a unit . . . • Type • Type_name = type definition; • e g: • Type • Scores = integer; • Averages = real • Won_lost = char;
Pascal Programming • The order of declarations . . . • Constants • Type • Variables • Procedures & Functions.
Pascal programming • Ordinal Types • A type with values that can be listed is called Ordinal Type. • An ordinal type’s value comes from its position in the list. So, an operator like < can be used to test its value. • Two ordinals produce a Boolean • 1<3 and a<d
Pascal Programming • The ordering of char follow the ASCII order (Appendix 11 in the text.) • Testing two ordinals can be useful when alphabetizing a list or files. • The operations of <, >, =<, => all work. • for loops naturally use ordinal type. The initial/final values must be of the same type as the variable. • e g: • for letter := ‘a’ to ‘z’ do • write (Letter)
Pascal Programming • Predefined functions deliver the previous (pred) and successive (succ) values. • ord will identify a value in a list—ord (9) returns 8 because the integer lists starts with 0.
Pascal Programming • Subrange Types • Subrange is a sub-set of an ordinal list. • A subrange type declaration always consists of two constant values separated by two dots. • e g: type • SmallInteger = -10 .. 10 • These two value are chosen from a host type. So, values of the subrange type have the same relationship as the host type. • A subrange can disclose an error that falls outside the expected range.
Pascal Programming • Enumerated Types • An enumerated type is a list of identifiers. • e g: • type • Awards =(bronze, silver, gold); • Type declarations can be mixed without an order.