1 / 15

Pascal Programming

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.

aram
Download Presentation

Pascal Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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.

  3. 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.

  4. 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.

  5. 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.

  6. 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.

  7. 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.

  8. 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.

  9. Pascal programming • 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 • The order of declarations . . . • Constants • Type • Variables • Procedures & Functions.

  11. 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

  12. 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)

  13. 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.

  14. 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.

  15. 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.

More Related