For next examples we will unfortunately need a little theory.

Declaration of variables

Pascal is a declarative language; if you like to use a variable or a procedure, you have to declare it first. For example, if you prepare a subroutine (in Pascal we use the term procedure), we can use it up from the point, where it is declared, to end of the program. This results to the fact that the main program (this one, which is really executed as the first) is written on the end of the program text. In our case, there will be no main program - this is generated by the Delphi to the separate file and is better to leave it untouched; Delphi programming means to create units only, where order of the procedure we will create is not as critical (this is an exception from this rule). But inside the procedure, this rule is due (we can use special means of Pascal to possible workaround, where we need it). For us it means in particular that each variable must first to be declared, before we will start to write the procedure itself.

Variable declaration begins with the keyword var. There are custom to write the keywords in bold, in manuscript underlined, it simplifies the readability of the text. After this keyword there are list of the definition of variables in the form of "variable - colon - type - semicolon". Pascal considers the newline character equal to space, so we need to mark end of each command by a semicolon (the same for variable declarations, procedures, etc.).We can both write multiple statements per line, or a command on multiple lines.

In a declaration it is possible to define multiple variables of the same type at the same time, their names are then separated by a comma. The actual declaration might then look like this:

var
  len1, width1 : integer;
  nameB : string;
  surname : pchar;
  address_a : string[33];

This example shows some of common types; most of them are generic and their meaning depends on the version of your Pascal compiler. They will be explained on the lectures, or if you are interested, you can visit Delphi help. In short:

integer represents an integer number in the range of -231 to 231-1 (for Delphi 3 to 7, at least). The memory size of this variable is 4 bytes.

char has not been present, but in represents one character. In real, there are two types of a character; ANSI and Wide. The first takes 1 byte from memory (with 256 possible values), the second takes 2 bytes (16 bits represents 216 possibly values (65536), but reasonable values are limited to the Unicode character set). For example the Delphi 4 is using the first (shorter) version, but you can change it by a compiler directive (as the rest of the generic types).

string means string of characters. We can index it as an array - for this we have to use the rectangle brackets, [ and ]. If you are using them in the declaration, we can define the maximal possible length of the string. When program runs, the length can change from zero to this limit. Whether your program check the range depends on the "Range check" setting of the compiler. Range checking is slow, but sometimes useful because save.
It is good to provide the string size during declaration, because if it is omitted, it will be either set to maximum (from the Delphi 3 version it is 64Ki, i.e. 65536 characters), or become variable (with the newest compiler). This not only consumes memory, but slows program at all (in the first case, mainly its execution).

For more information about strings, you can visit for example Delphi.About.

The main pchar disadvantage is, that you have to ask system for memory before first use; the reason is, that pchar comes from C. Sometimes this type is used directly in Delphi libraries; in this case, the memory reservation is done by Delphi itself. This type is essential for use libraries with a C origin, i.e. API.

Assignment statement

This is one of most important commands, in the Pascal in the form of:

<variable> := <expression> ;

We will postpone the expressions to the next lesson, so for today, expression means either another variable, or a constant. I hope, that integer number is well known to everybody, so for now - spot to strings description.

String will be written using following rules:

There are few examples:

  jmeno := 'Aleš Černý';
  jmeno := 'Adam''s voice';
  jmeno := 'Adam'#27's voice';
  jmeno := 'blue'^I'black'^I'white';
  jmeno := 'blue'#9'black'#9'white';
  jmeno := #65#100#97#109;

Second and third examples are the same, as is the fourth and fifth example. If you need to write a character, write string of length of one.

If you need to write the end-of-line characters, use #13#10 (nearly not usable in the Delphi) (CR, LF - in the Linux environment (Kylix, Lazarus) use the LF only, #10).

For explaining of variable construction of the Delphi, we need to explain the objects (OOP) in advance:

Record type declaration

You can declare a new type on the base of the existing types. We can do it in the type chapter. As you can see from the previous text, the type chapter has to be before the var chapter, after which the function declaration should follow. In any declarative (or "imperative") language, you have to use this order. In some special cases, where it is not possible, you can use some technical directives to translator, when you are using different order.

Type declaration: For example, if you are often using a string with 37 letters, you can declare a special type for them. For example, instead of

var
  pol1,pol3 : string[37];

you can write

type
  hbk = string[37];
var
  pol1,pol3 : hbk;

It can make a program more readable, if you use something more meaningful, then in previous example.

There is a custom to use the "t" as a first character of the name of the type:

type
  Tpol = string[37];

Summary: in the type chapter, there are one or more lines with the new type declaration, finished by semicolon.

You can construct a new type as a combination of more types. For example, we can keep information about one employee in one variable, containing name (33 characters length string), surname (we choose 37 characters maximum), size and weight (it is not legal now to measure employees, but this is typical example). We will use integer number for size (EU - centimeters). For this, we can connect all parts of information in a new type of variable, which is the record type. We will write a name of the new type, then keyword record, then we will write list of declaration of individual parts of the record (in the same form, as the variable declaration, do not forget semicolons), and then we will write another keyword, end, to mark the and of declaration. Then, in the var chapter, we can use the new type:

type
  Tperson = record
    name : string[33];
    surname : string[37];
    size, weight : integer;
  end;

var
  person, person2 : Tperson;

For use the new variables in the program, use the dot (".") character to select the appropriate part of the variable, for example:

person.name := 'Peter';
person2.size := person.size;

We can use even this type of variable in another type declaration. Imagine, that you need (for some specific employee) more information (salary, remaining paid leaves of absence, ...). We can create new, more complex type:

type
  Tperson = record
    name : string[33];
    surname : string[37];
    size, weight : integer;
  end;
  Templ = record
    em : Tperson;
    salary, rem_paid_abs : integer;
  end;

var
  person, person2 : Tperson;
  z1,z2,z3 : Templ;

Now, for using "name" for variable "z1", we need to denote this string by to dots ("."), because it is inside the original record:

  z1.em.name := 'George';

Because this example was very common, there has been proposed new solution - the "object" type. The object can inherit (descent) previous declaration, just using another form of declaration (instead of record we will now create object, eventually followed by the parents type in the brackets):

  Tperson = object
    name : string[33];
    surname : string[37];
    size, weight : integer;
  end;
  Templ = object(Tperson)
    salary, rem_paid_abs : integer;
  end;

var
var
  person, person2 : Tperson;
  z1,z2,z3 : Templ;

As the result we can rewrite the previous example as:

  z1.name := 'George';

The same method is used by the Delphi for its only variable in the each user-generated unit, a you should try:

Task:. Try to create new program (for example, restart Delphi) and place here a button (Button1), label (Label1) and an edit box (Edit1). This form will use the only variable for all of this objects, by adding them to the Form1 structure (note: you can place objects such a button or edit by drag-and-drop them from the Standard toolbar (palette) ). After creating the required form (by visual programming way), you will get structure like:

type
  TForm1 = class(TForm)
    Label1: TLabel;
    Edit1: TEdit;
    Button1: TButton;
    private
      { Private declarations }
    public
      { Public declarations }
  end;

Keyword object has been substituted by the keyword class. Classes have some other advantages against objects, but not important for us.

The TForm1 structure is easily readable, you can find a button, edit or label, even in the order, how they have been placed. Please do not forget, how they have been created - if you like to erase an object, you have to delete it using the graphics interface, not here. If you accidentally erase something, it is often easier to copy everything to a new application, than to try to repair it.

Example, how to use the object: If you know (from the documentation?), that the TLabel text is written by Font (which it contain), and the Font is a structure, containing Color, you can change the color of text on the Label1 by:

  Form1.Label1.Font.Color := clRed;

clRed is a Delphi constant.

You can discover parts of the structure of an object using the Object Inspector (F11) (see the previous lesson), when you select the Property fold. There are only these properties, which can be set before program execution (many of them are run-time only, so they will be accessible only by program after execution). For complete description of the pre-defined Delphi object, you can place a cursor (in the source text) to the object type name (i.e. TButton, TEdit) and press Ctrl+F1. It will raise (evoke?) help with description of the structure.

We have used the handling creation by double-click on the object last week, which moved us to the appropriate place in the source code (again, if you generate an unwonted procedure, don't erase it in the program code; if you like to erase procedure, you don't like any more, erase all other than generated lines, and the Delphi environment will erase it in the first run of the compiler. This week, we will solve the following task:

If anyone (=user) press the button, the Edit1 content will be copied to the Label1 text.

Solution: Using doubleclick on the button, generate procedure. Between begin and end line, place the correct command. Because of the Edit content is accessible as the Text property and for the Label, the text is in the Caption property (see Object Inspector), the correct command will be:

Form1.Label1.Caption := Form1.Edit1.Text;

In 308, remember, that you have to save the project (Save Project as...) to an accessible directory (for example, your directory on the desktop), and then execute and test it (try to change a text in the edit and then press a button).

If you have more time:
In this case, we can move the text on-line; to keep the first function working, we can find another destination. The window heading is a good example; this text is in the Form1.Caption variable. Try to doubleclick on the edit box and create a program, which will copy the Edit1 content to the Caption property, but of the Form1 object.

Comments

The Pascal keeps the originally proposed standard, where programmer can use the small (lowercase) or capital letters as he like without any problem. This rule has been proposed for Algol (this first language of this type has been created by users (mathematics), not by system programmers), but it needs from the compiler to convert everything to -for example- capital letters internally before looking to the table (containing variable names). Because of another language authors are lazy, many of then lack this feature, so their programmers have to keep the small/capital letters as they declared. In most of this text, I'm try to write the variable/object names correctly, but you can write them, as you like (most popular is all lowercase).

In the Pascal, each command will be finished by semicolon. Note, that you don't need to place a semicolon before the end command. If you write more semicolons than you need, or more parenthesis, the resulting code will be probably the same (advantage of this high quality Borland compiler).

Sometimes, some words have to be split by separator. A separator is typically a space, tabulator or end of a line. There are no difference between these three characters - everywhere, where can be space as a separator, can be end of line (the new line inserted by the Enter key). Space in another function then separator is only between apostrophes in the text constant (strings).

If you like to write a comment, you should use the curled brackets ( { and } ). If you like to tell to the compiler not to translate some part of your program, you can mark this part as a comment. If you already use curled brackets, you can use the combination of parenthesis and star, i.e. (* and *), which has higher priority. If the compiler finds the first combination, then it skips everything to the *).
Comment can starts with two slashes ( // ) as well, then it ends on the end of the line.

There are some important rules to keep program readable, for example the indent of the lines, which are somehow internal, see examples in this course. Another good custom of Pascal programmers are use of very long variable identificators. While in assembler, the underscore ( _ ) character has been often used, Borland comes with preference of a capital letters, for example CompAvgPriceOnStack instead of comp_avg_price_on_stock (the C++ programmer would prefer an identifier like cavg for this).

Solved tasks in the "edit" (in Czech). Sorry to name of the project.