Built-In Routines

 

General Routines

 

Introduction

Although as a smart programmer you can create any routine to perform a desired job, the Object Pascal language provides a series of procedures and functions already made so you can just add them to your program without caring how they work, all you need to know is what these routines do and in what unit they are defined. The procedures and functions that are part of the Object Pascal language are highly valuable, were tested sufficiently, and are completely reliable. The Object Pascal built-in procedures and functions are made for various assignments ranging from algebra, geometry, trigonometry, and finance, etc.

The Size Of a Variable

We mentioned that, when you declare a variable, the compiler reserves an amount of space in the computer memory for that variable. We also illustrated how much space some data types need to store their variable. Object Pascal provides the SizeOf function that allows you to find out how much space a data type or a certain variable in your program is using. The syntax of the SizeOf function is:

function SizeOf(X): Integer;

There are two techniques of using the SizeOf function: using the variable or the data type. To find out how much space a data type, such as Integer, Single, or Double, etc would occupy in memory, pass it as the argument to SizeOf. The SizeOf function produces the number of bytes that its arguments is using in memory. Here is an example:

program Project1;

{$APPTYPE CONSOLE}

begin
	Writeln('An integer occupies ', SizeOf(Integer), ' bytes');
	Write('Press any key to continue...');
	Readln;
end.

This would produce:

An integer occupies 4 bytes
Press any key to continue...

On the the other hand, to know how much space a variable of your program is occupying in memory, pass its name as argument to SizeOf. Here is an example:

program Project1;

{$APPTYPE CONSOLE}

var Distance: Double;
begin
	Distance := 248.74;
	Writeln('The Distance variable occupies ', SizeOf(Distance), ' bytes');
	Write('Press any key to continue...');
	Readln;
end.

This would produce:

The Distance variable occupies 8 bytes
Press any key to continue...

 

ASCII Characters

Object Pascal provides the Chr function used to get the ASCII character corresponding to a specified Byte number. Its syntax is:

function Chr(X: Byte): Char;

A number of size Byte (between 0 and 255) must be passed to the Chr function. The function then returns the mapped character. If the character is readable, it can be retrieved with this function. For example, you can display the character on a console window. Here is an example:

program Project1;

{$APPTYPE CONSOLE}

begin
	Writeln('Character at 68: ', Chr(68));

	Write('Press any key to continue...');
	Readln;
end.

This would produce:

Character at 68: D
Press any key to continue...

Some characters of the ASCII map are not readable. For example, the Enter key corresponds to 10. In a case a character is not readable, it can still be used one way or another. For example, you can use Chr(10) to send the caret to the next line. That is we will regularly use it from now on.


Bytes Moving

In previous lessons, we learned to assign a value to a variable or one variable to another. Object Pascal provides an alternative to this. It consists of copying bytes from one variable into another. The procedure used is called Move and its syntax is:

procedure Move(const Source; var Dest; Count: Integer);

The Move procedure takes three arguments. The first represents the variable whose bytes will be copied. The second argument is the variable that will receive the bytes previously copied. The third argument specifies the number of bytes of Source that will be copied into Dest. Here is an example:

program Project1;

{$APPTYPE CONSOLE}

var
	Person1 : string;
	Person2 : string;

begin
	Person1 := 'James Waters';
	Writeln(Person1);
	Move(Person1, Person2, SizeOf(Person2));
	Writeln(Person2);

	Write(Chr(10), 'Press any key to continue...');
	Readln;
end.

This would produce:

James Waters
James Waters

Press any key to continue...

The Lowest Value of a Type

When we studied enumerators, we found out that each member of the array receives a constant value. The members of an enumerator are labeled internally so the first has an index of 0, the second is 1, etc. In fact, Object Pascal provides the Low function that allows you to know the index of the first member of an enumerator. Its syntax is:

function Low(X);

Normally, the Low function can be applied to other types of lists (such as arrays that we will study in future lessons). The Low function takes as argument a variable declared from a type (or array) and returns the index of its first member. In the case an enumerator type, it would return 0. Here is an example:

program Project1;

{$APPTYPE CONSOLE}

type Players = (GoalKeeper,
		RightDefender, Stopper, Libero, LeftDefender,
		MiddleLeft, MiddleCenterLeft, MiddleCenterRight, MiddleRight,
		Forward1, Forward2);

var LowValue: Players;
    Natural: Integer;

begin
	LowValue := Low(Players);
	Natural := Ord(LowValue);

	Writeln('The lowest value of the Players type is ', Natural);

	Write(Chr(10), 'Press any key to continue...');
Readln;
end.

This would produce:

The lowest value of the Players type is 0

Press any key to continue...

The Highest Value of a Type

As opposed to finding the index of the first member of a list such as an enumerator, Object Pascal provides the High function. Its syntax is:

function High(X);

The High function returns the integer value of the last member of an enumerator type. Here is an example:

program Project1;

{$APPTYPE CONSOLE}

type Players = (GoalKeeper,
		RightDefender, Stopper, Libero, LeftDefender,
		MiddleLeft, MiddleCenterLeft, MiddleCenterRight, MiddleRight,
		Forward1, Forward2 );

var HighValue: Players;
    Natural: Integer;

begin
	HighValue := High(Players);
	Natural := Ord(HighValue);

	Writeln('The lowest value of the Players type is ', Natural);

	Write(Chr(10), 'Press any key to continue...');
	Readln;
end.

This would produce:

The lowest value of the Players type is 10

Press any key to continue...

The Ordinal Value of a Type

To get the index, also called an ordinal value, of the member of an enumerator, you can call the Ord function. Its syntax is:

function Ord(X): Longint;

The Ord function takes as argument the member of an enumerator and returns its numeric place in the list. Here is an example:

program Project1;

{$APPTYPE CONSOLE}

type Players = ( GoalKeeper,
		 RightDefender, Stopper, Libero, LeftDefender,
		 MiddleLeft, MiddleCenterLeft, MiddleCenterRight, MiddleRight,
		 Forward1, Forward2 );

var Ordinal: Integer;
begin
	Ordinal := Ord(LeftDefender);
	Writeln('The ordinal value of LeftDefender is ', Ordinal);

	Write(Chr(10), 'Press any key to continue...');
	Readln;
end.

In the same way, you can get the ordinal value of any member of an enumerator type.

 

Mathematic Routines

Introduction

The Pascal language provides various routines that can be useful in program processing. Some of these routines tend to perform simple operations such as incrementing or decrementing variables. Additionally, Pascal inherently defines general functions used to perform regular operations on numbers. Such operations include trigonometry, arithmetic, etc. Besides the various routines of the Pascal language, Borland created an incredibly large number of functions for the Delphi programming language. Because this book is primarily an introduction to Delphi, we will cover only the routines that are more likely part of the Pascal language. The others, related to Delphi and hardly if ever used in this book, will not be reviewed.

Value Incrementing

While the C/C++ language provides an operator to increment the value of a variable, Object Pascal uses a procedure instead. Its is called Inc and its syntax is:

procedure Inc(var X [ ; N: Longint ] );

The Inc procedure takes one required argument and one optional. The X parameter can be the name of an initialized variable. After calling Inc, the value of X is incremented by 1. Here is an example:

program Project1;

{$APPTYPE CONSOLE}

var Natural: Integer;

begin
	Natural := 158;
	Writeln('Natural = ', Natural);
	Inc(Natural);
	Writeln('Natural = ', Natural);

	Write(Chr(10), 'Press any key to continue...');
	Readln;
end. 

This would produce:

Natural = 158
Natural = 159

Press any key to continue...

By default, the Inc procedure is used to increment the value of a variable by 1. If you want to increment the value by more than 1, pass a second argument. The optional second argument allows you to specify by how much X would be incremented. In the following example, the argument, Natural, is increased by 14:

program Project1;

{$APPTYPE CONSOLE}

var Natural: Integer;

begin
	Natural := 158;
	Writeln('Natural = ', Natural);
	Inc(Natural, 14);
	Writeln('Natural = ', Natural);

	Write(Chr(10), 'Press any key to continue...');
	Readln;
end.

This would produce:

Natural = 158
Natural = 172

Press any key to continue...

Value Decrementing

As opposed to incrementing, you may want to decrement the value of a variable. This is taken care of by the Dec procedure. Its syntax is:

procedure Dec(var X[ ; N: Longint]);

The Dec procedure functions opposite to Inc. It is used to reduce the value of the argument, X, by one. In other words, it works as X = X – 1. The first argument is required.

The second argument is optional and allows you to specify how much the value of the first argument should be decremented.

Here is an example:

program Project1;

{$APPTYPE CONSOLE}

var Natural: Integer;

begin
	Natural := 245;
	Writeln('Natural = ', Natural);
	Dec(Natural);
	Writeln('Natural = ', Natural);
	Dec(Natural, 122);
	Writeln('Natural = ', Natural);

	Write(Chr(10), 'Press any key to continue...');
	Readln;
end.

This would produce:

Natural = 245
Natural = 244
Natural = 122

Press any key to continue...

The Integral Value of a Real Number

A fractional decimal number is made of two parts: the integral section and the decimal section. An example of such a number is 14522.905

To get the value of the integral part of a floating-point number, you can call the Int function. Its syntax is:

function Int(X: Extended): Extended;

Here is an example of using the function:

program Project1;

{$APPTYPE CONSOLE}

var Number, Natural: Double;

begin
	Number := 1422.905;
	Natural := Int(Number);

	Writeln('The integral part of ', Number:9:3, ' is ', Natural:5:3);

	Write(Chr(10), 'Press any key to continue...');
	Readln;
end.

This would produce:

The integral part of 1422.905 is 1422.000

Press any key to continue...

The Fractional Value of a Real Number

To get the decimal value of a real number, you can call the Frac function. Its syntax is:

function Frac(X: Extended): Extended;

Here is an example of using it:

program Project1;

{$APPTYPE CONSOLE}

var Number, Natural: Double;

begin
	Number := 1422.905;
	Natural := Frac(Number);

	Writeln('The integral part of ', Number:9:3, ' is ', Natural:5:3);

	Write(Chr(10), 'Press any key to continue...');
	Readln;
end.

This would produce:

The integral part of 1422.905 is 0.905

Press any key to continue...

Value Rounding

Rounding a value consists of finding the closest natural value to its real value. To perform this operation, you can call the Round function. Its syntax is:

function Round(X: Extended): Int64;

The Round function takes as argument a number. If the number is an integer, the function would return its value. If the number is real, the function returns the integer that is closest to making it whole. Here is an example:

program Project1;

{$APPTYPE CONSOLE}

var Number: Double;
    Rounder: Integer;

begin
	Number := 1422.905;
	Rounder := Round(Number);

	Writeln(Number:6:3, ' rounded to the nearest number is ', Rounder);

	Write(Chr(10), 'Press any key to continue...');
	Readln;
end.

This would produce:

1422.905 rounded to the nearest number is 1423

Press any key to continue...

Number Truncating

While the Round function can help you round a real number to its nearest integer, at times, you may want to find the lowest integer that is closest to a real number. This operation can be taken care of by calling the Trunc function. Its syntax is:

function Trunc(X: Extended): Int64;

The Trunc function returns a value that is the first integer less than the value passed as argument. Here is an example:

program Project1;

{$APPTYPE CONSOLE}

var Number: Double;
    Truncate: Integer;

begin
	Number := 1422.905;
	Truncate := Trunc(Number);

	Writeln(Number:6:3, ' truncated to the nearest integer is ', Truncate);

	Write(Chr(10), 'Press any key to continue...');
	Readln;
end.

This would produce:

1422.905 truncated to the nearest integer is 1422

Press any key to continue...

Arithmetic Functions

The Object Pascal language also provides a series of functions to perform various mathematically related operations. The functions are defined in various libraries and this can depend on the compiler you are using.

Some of the available functions are:

AbsCeilExpFloor FrexpLdexp Log2log10 LogNLnLnXP1 IntPowerPowerSin SinhSqrSqrt

5.2.9 Trigonometric Functions

Some of the trigonometric functions offered by Object Pascal are:

ArcTan
ArcTan2
CosCosh
Sin
SinCos
Sinh
Sqrt
Tan
Tanh

Object Pascal provides many other functions used for the Delphi programming environment.

Hexadecimal Routines

The Low Order Byte

Object Pascal provides various functions related to hexadecimal numbers. To get the low order byte or a word, a double-word, or a quad-word number, you can call the Lo function. Its syntax is:

function Lo(X): Byte;

The High Order Byte

To get the high order byte or a word, a double-word, or a quad-word number, you can call the Hi function. Its syntax is:

function Hi(X): Byte;

Strings Routines

String Copying

So far, to initialize a string, we were using the assignment operator to asign a string value to a string variable. Here is an example:

program Project1;

{$APPTYPE CONSOLE}

var
	Team: string;

begin
	Team := 'Manchester United';

	Writeln('Team: ', Team);

	Write(Chr(10), 'Press any key to continue...');
	Readln;
end.

This would produce:

Team: Manchester United

Press any key to continue...

Alternatively, Object Pascal provides the Copy() function used to copy one string into another string. The syntax of the Copy() function is:

function Copy(S; Index, Count: Integer): string;

This function takes three arguments. The first argument, S, is the string that you are trying to replace. The Index parameter is the index of the first character from the string that will be copied. The Count parameter specifies the number of characters that will be copied.

Here is an example: 

program Project1;

{$APPTYPE CONSOLE}

var
	Team, Result: string;

begin
	Team := 'Manchester United';
	Result := Copy(Team, 0, 40);

	Writeln('Team: ', Result);

	Write(Chr(10), 'Press any key to continue...');
	Readln;
end.

This would produce:

Team: Manchester United

Press any key to continue...

The Length of a String

In many operations, you will want to know how many characters a string consists of. To find the number of characters of a string, you can call the Length() function. Its syntax is: 

function Length(S): Integer;

The Length() function takes one argument, which is the string you are considering. The function returns the number of characters of the string. Here is an example:

program Project1;

{$APPTYPE CONSOLE}

var
	Team: string;
	Len: Integer;

begin
	Team := 'Manchester United';
	Len := Length(Team);

	Writeln(Team, ' contains ', Len, ' characters');

	Write(Chr(10), 'Press any key to continue...');
	Readln;
end.

This would produce:

Manchester United contains 17 characters

Press any key to continue...

String Addition

String addition consists of adding one string to another. This can be taken care of using the addition operator, +. The addition of two strings produces a new string you can use as you see fit. Here is an example:

program Project1;

{$APPTYPE CONSOLE}

var
	Name1, Name2, Result: string;

begin
	Name1 := 'Manchester ';
	Name2 := 'United';
	Result := Name1 + Name2;

	Writeln('Team: ', Result);

	Write(Chr(10), 'Press any key to continue...');
	Readln;
end.

This would produce:

Team: Manchester United

Press any key to continue...

String addition is also referred to as concatenating two strings. It can be performed using the Concat function. Its syntax is:

function Concat(s1 [, s2,..., sn]: string): string;

The Concat() function takes at least one argument. If you supply only one argument, the function returns the same string, S1. Normally, that is not how you would use the Concat function. Instead, to add string, pass each as the second, third, etc, argument. The second argument would be added to the first. If there is a third, it would be added to the second. A fourth would be added to the third, etc. Here are two examples:

program Project1;

{$APPTYPE CONSOLE}

var
	Name1, Name2, Result: string;
	Country : string;

begin
	Name1 := 'Manchester ';
	Name2 := 'United';
	Result := Concat(Name1, Name2);

	Country := 'Emirats';
	Country := Concat(Country, ' ');
	Country := Concat(Country, 'Arabes');
	Country := Concat(Country, ' ');
	Country := Concat(Country, 'Unis');

	Writeln('Team: ', Result);
	Writeln('Country: ', Country);

	Write(Chr(10), 'Press any key to continue...');
	Readln;
end.

This would produce: 

Team: Manchester United
Country: Emirats Arabes Unis

Press any key to continue... 

The Position of a Substring

A substring is a section that is part of a string. Various operations and/or functions allow you to access and use substrings. To get the position of a substring from a string, you can call the Pos function. Its syntax is:

function Pos(Substr: string; Str: string): Integer;

The Pos function takes two arguments. The Str argument represents the main string that will be considered. The SubStr parameter is a string to look for in Str. If Substr is found as part of Str, the Pos function returns the ordinal position of the first character.

Substring Insertion

Another type of operation you can perform on a string consists of inserting another string to it. This can be done using the Insert procedure. Its syntax is:

procedure Insert(Source: string; var Str: string; Index: Integer);

The Source parameter is the main or original string that will be considered for this operation. The Str parameter is the string that will be inserted into Source. The insertion will start at Index, which is a position inside of Source.

Substring Deletion

As opposed to adding or inserting a string to another, if you want to remove a character or a section from an existing string, you can call the Delete procedure. Its syntax is:

procedure Delete(var Source: string; Index, Count: Integer);

The Source param2ter is the string involved in the operation. The Index is the position of the character where the operation would start. The Count value specifies the number of characters to be removed from Source.




Previous Copyright © 2004 FunctionX, Inc. Next