The implementation Section




The implementation section starts with the implementation keyword and ends with the next unit keyword. The next unit keyword is usually the unit's final end keyword, but could be the initialization ke yword in units that have an initialization section. It's difficult to say more than that right now, because there are other aspects of Pascal that I need to discuss before tying all of this together. However, let me give you an example that will illustrate the use of the interface and implementation sections.

Let's say that you create a unit that has a procedure called DoSomething. Let's further say you want DoSomething to be available to other units in your project. In that case, you would declare the DoSomething procedure in the interface section and then define the procedure in the implementation section.

DoSomething procedure is declared in the interface section and defined later in the implementation section. I realize I'm getting a little ahead of myself here. Functions and procedures will be discussed more tomorrow, and I'll go over declarations and definitions in detail at that time.

The initialization and finalization Sections

The initialization and finalization sections can be used to perform any startup and cleanup code that a unit requires. Any code in the initialization section will be executed when the unit is loaded into memory. Conversely, any code in the finalization section will be executed just before the unit is unloaded from memory. You can have just an initialization section, but you cannot have a finalization section without an initialization section. The initialization and finalization sections are optional.

The const Keyword

A unit can optionally have one or more const sections. The const section is designated with the const keyword. The const section describes a list of variables that are known as constants.

A constant is an identifier that cannot change. For example, let's say you have certain values that your program uses over and over. You can set up constant variables for those values. To illustrate, let's add a const section to the program in Listing 1.3. You'll add one const section for constants that are public (available to other units) and another const section for constants that are available only to this unit.

Because the AppCaption constant is declared in the interface section, it can be used anywhere in the unit and in any unit that has this unit in its uses list. The BaseX and BaseY constants, however, are only available within this unit because they are declared in the implementation section.

The type Keyword

Declaring a new type is an esoteric programming technique that is difficult to explain at this stage of the game, so perhaps an example will help. Let's say that your application needs an array (a collection of values) of 20 bytes and that this type of array will be used over and over again.

/Now you can use the identifier TMyArray instead of typing out array [0..19] of Byte every time you want an array of 20 bytes. I'll have to leave it at that for now, but you'll see more examples of declaring types later in the book.

Comments in Code

/Before getting into the Pascal language in detail, let me talk briefly about commenting code. Comments are lines of text in your source code that are there for documentation purposes. Comments can be used to describe what the code does, to supply copyright information, or simply to make a note to yourself or other programmers.

 

Variables

/Variables have to be declared before they can be used. You declare a variable in a special section of code designated with the var keyword, as described earlier--for example,

/After you declare a variable, you can then use it to manipulate data in memory. That probably doesn't make much sense to you, so let me give you a few examples. The following code snippet uses the variables called X and Y declared earlier. At the end of each line of code is a comment that describes what is happening when that line executes.

/A variable is a location set aside in computer memory to contain some value.

/I want you to notice several things about this code. First, notice that the value of X changes as the variable is manipulated. (A little later I'll discuss the Object Pascal operators, functions, and procedures used to manipulate variables.) You can see that the variables are assigned values, added together, incremented, and so on.

/Notice also that each statement in this code segment ends in a semicolon. The semicolon

is used at the end of every statement in a Pascal program.



Поделиться:




Поиск по сайту

©2015-2024 poisk-ru.ru
Все права принадлежать их авторам. Данный сайт не претендует на авторства, а предоставляет бесплатное использование.
Дата создания страницы: 2016-04-11 Нарушение авторских прав и Нарушение персональных данных


Поиск по сайту: