Delphi Programming for Beginners - Lesson 6

Minimum of Object Pascal

Let’s go back to the basics of Object Pascal.

Structure of the program

Object Pascal is a strictly type procedural language which is made for both structured and object oriented programming. Instructions are divided by ; and blank symbols (spaces, tabulators, new lines are equal to a space - unless they are a part of a chain), groups of instructions begin with keyword begin and end with keyword end.

Program is divided into functions (or procedures), which can be grouped into a class. Procedures and functions or classes are saved in units. The whole program consists of files with a key word program and additional units. The smallest program has only one file with key word program - in Delphi usually a .dpr file. Unites are saved in *.pas files. Unit usually has 2 parts - interface and implementation.


unit Jednotka;
 interface
 uses Jednotka2;
  procedure Test;
 implementation
 uses Jednotka3;
  procedure Test;
   begin
     ShowMessage('test');
   end;
end.

In interface we specify everything what should be available from other units. In implementation part we implement everything. Parts with uses are voluntary. If it’s possible, we put uses into implementation - thus we make the control of relations easier for a compilator.

Variable types Ordinal types

Integer numbers, char, boolean and enum are included in ordinal types.

Numeric types

This includes integer and real types. Integer types (byte, word, integer, cardinal, longint, int64,..) vary by size and whether they are signed or unsigned. Some of them have set their size, others are generic. Generally, you should use Integer (unless you really need a different type). To work with hexadecimal numbers, use prefix $, for example $00A.

Real types vary by size (single, double, extended) and thus by accuracy. Original type from Pascal is now mapped to double, if you really want to use the original one, use Real48. Generally, you should use double.

Integer types support mod and div, that is integer dividing, as well as and, or, xor, not and shr and shl. You can as well use function Odd, which returns, whether the argument is odd.

Conversion between real and integer types: round return rounded number, trunc return the integer part of a real type number, frac returns the part after a dot. Arithmetic functions: Abs, ArcTan, Cos, Sin, Ln, Pi, Sqr, Sqrt, Exp. You can find other functions in Math unit.

Boolean type

This type represents a logical status (true or false). It’s saved as a byte. It supports logical operations and, or, xor, not. Generally speaking, a compilator supports a shortened evaluation - that is, it there’s a more complicated expression and at a certain moment the result is obvious - the remaining part of the expression is not evaluated. So:

  • the fast part of the expression should be in the beginning
  • if there’s a call for function in an expression, it may not be executed

Char type

This type represents a symbol. Until Delphi 2007 (including) it is a byte-sized (values 0-255), in following unicode versions 2 bytes. A constant is defined as (for example) 'zz'' or #66 where 66 is an ASCII code. Conversion to Char is done via Chr and backwards by Ord.

String type

String is a chain of chars. Generally speaking, there are several types of string, but we will work with the general one only.

There are many functions for working with string such as StringReplace, Pos, Copy, Delete, Length, QuotedStr, Trim (removes white blank symbols) and others. String is very intelligently implemented and a compiler does a lot of magic so the work with it is quick and pleasant.

Enums

This type is used pretty often in VLC. For example:


TFormStyle = (fsNornam, fsMDIChild, fsMDIForm, fsStayOnTop);
TPismeno = 'A'..'Z';
A convention is that a value of an enum type begins with a first letter of type’s name - fsNormal. A variable is saved as an integer.

Sets

Set is a group of values of one ordinal type and that type can’t have more than 255 values. For example:


TSetPismeno = set of TPismeno;
var
 Znak: TSetPismeno;
begin
 Znak := ['A','B','C'];
 if ‘A’ in Znak then …

You can use operators + and - or functions Include and Exclude for adding values to a set.

Records

It’s a structured data type with a declaration


type TZaznam = record
      iPolozka1:integer;
      sPolozka2:string;
     end;

In newer versions of Delphi, the declaration was extended with a possibility of calling procedures and functions, for example:


type
 TMyRecord = record
  var
Red: Integer; procedure printRed(); end; var zaznam: TZaznam; zaznam.iPolozka1 := 10;

Often it’s used with a key word with as seen in following example:


with zaznam do
begin
  iPolozka1 := 10;
end;

Procedural types

This most difficult type is used for controlled calling. It’s a basic element of VCL where all events such as ButtonClick are declared as a procedural type. We use it as well for calling functions in DLL libraries.

property OnClick: TNotifyEvent where declaration TNotifyEvent = procedure(Sender: TObject) of object; it means method of the object with one parameter (Sender) so procedure Button1Click(Sender: TObject);

The advantage of this approach is that we can declare a variable of TNotifyEvent type or a parameter and then work with it and then call whenever we really need it.

For instance, mTest(Button1Click); where


procedure TForm1.mTest(pClick: TNotifyEvent);
 begin 
   pClick(Self); 
 end;

Arrays

An array is a structured data type consisting of one of previous types (including records or string). All items are saved one by one. An array here doesn’t have to start with 0, the only limitation is that the interval has to be an ordinal type (that includes enum or char).


type TPole = array [1..10] of integer;
var pole: TPole;
pole[1] := 123;

It’s also possible to not specify a limit, such array is then called open. An array of any kind of size can be put into it. A size of an array can be find out by calling Length, low by calling Low and high by calling High>


function Sum( var X: array of Double): Double;
 var
  I: Word;
  S: Double;
 begin
  S := 0;
  for I := 0 to High(X) do S := S + X[I];
  Result := S;
 end;
Homework

Create an unit for an array of complex numbers.


TComplex = record
             im, re: double; 
           end;

An unit should have at least 2 functions, Add and Sub (basically + and - operators applied on all numbers in an array), a parameter is an array TComplex and a result is a number TComplex. Finally, you should test it by calling from main program.

About the Author

Ing. Radek Červinka
Embarcadero MVP
Web: Delphi.cz
Twitter: @delphi.cz

More interesting links and pages


All binary option robots reviewed


Valforex.com Reviews


Breaking News & Headlines


FX Tribune - Breaking Daily News


USA News


DailyCrosswordSolver.com


CodyCrossAnswers.com


Puzzle Shuffle


SudokuSolver.net


BestVPNRating.com