1 / 13

Turbo Pascal Units (TPU)

Turbo Pascal Units (TPU). Brent M. Dingle Texas A&M University Chapter 8 – Section 1 (and some from Mastering Turbo Pascal 5.5, 3 rd Edition by Tom Swan). What is a Unit?.

jana
Download Presentation

Turbo Pascal Units (TPU)

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. Turbo Pascal Units (TPU) Brent M. DingleTexas A&M University Chapter 8 – Section 1 (and some from Mastering Turbo Pascal 5.5, 3rd Edition by Tom Swan)

  2. What is a Unit? • A unit is a collection of procedures, functions and defined constants (and other stuff) that can be compiled apart from any program. • The procedures, functions and constants that are defined in a unit can then be used by any program you write in the future, without having to be declared in the program. • The file extension for Turbo Pascal Units is .TPU.

  3. What makes a Unit? • Units are written in a similar fashion to programs. • They are divided into four parts (note the book i.d.s only 2 of them to begin with but all four are mentioned eventually) • Unit Declaration/Heading • Unit Interface Section • Unit Implementation Section • Unit Initialization

  4. Unit Declaration • This is like the declaration or heading line of the program, it gives the unit a name. • For other programs to access the unit they will need to have a line of code which says: USES [unit name]where [unit name] is the name of the unit they wish to access. • It is best to name units the same as their filename (without the .tpu extension). See comment on File names, page 278.

  5. Unit Interface • This is the public section of the unit. • It describes all the features inside a unit that it can share with programs or other units. • It contains labels, constants, types, variables along with function and procedure declarations. • Think of this as describing how someone might ‘interface’ with the unit to use it.

  6. Unit Implementation • This is the private section of the unit. • This part contains the actual statements which implement the procedures and functions declared in the interface section. • Constants, types and variables may also be placed in this section but they will only be able to be used by this unit. Call these items private constants, private types, private variables. • You may also declare private functions and private procedures, by placing them here and omitting them from the interface section.

  7. Initialization • This is an optional block of statements very similar to the main body of a Pascal program. • The statements found here will run before any statements in the program that uses the unit. • This allows the unit to initialize its own variables and perform other tasks before the host program even begins. • Many units leave this section empty/blank.

  8. Structure of a Unit File UNIT [name]; INTERFACE { USES declarations go here } { CONST, TYPE and VAR declarations go here } { Procedure and Function declarations go here } IMPLEMENTATION { private CONST, TYPE and VAR declarations go here } { Place Procedure and Function bodies here } BEGIN { optional } { Initialization statements go here } END.

  9. Bizarre Fact • Since the Initialization section is optional the BEGIN in the unit is also optional. • However the END. is required. • This is the only time you will have an END without a BEGIN.

  10. Common Predefined Unit • You probably have already been using the clrscr function. • This function is part of the CRT unit. • The CRT unit contains several different functions and procedures for accessing the monitor (once called the CRT device = Cathode Ray Tube device).

  11. Compiling Units • Your TA’s should show you how to compile a unit in lab. • The unit BooleIn described on page 282 and 283 along with the corresponding program TestBooleIn might be a good place to start. • If they are nice they will have already typed them up and placed them somewhere for download for you – so all you need do is compile and test them. • Hopefully this won’t take more than 20 minutes.

  12. Suggested Problems (not graded) • page 290: • 1, 4, 5, 6, 7 • think about 2 and 3

  13. End Units

More Related