Using Procedures


Using the Write and WriteLn procedures is really simple because they are built into the Delphi compiler. Only a small number of procedures are built into the compiler. The vast majority of procedures and functions can be found in separate source code files called units. All Delphi units have the extension .pas.

Before we can use a procedure in our application, we have to know three things about it: its name, the unit where the procedure is declared, and the parameters the procedure takes. The name of the procedure and the parameter list are parts of the procedure header. A simple procedure header looks like this:

procedure ProcedureName;

The reserved word procedure is always followed by a procedure name, which is any valid identifier. This procedure has no parameters. A procedure header with a parameter list looks like this:

procedure ProcedureName(ParameterList);

The parameter list is a mechanism for passing values to procedures (and functions). The parameter list can contain one or more parameters. If the parameter list contains more than one parameter, they are separated by semicolons. Here is the header of a procedure that accepts a single string value:

procedure DisplayString(s: string);

The procedure DisplayString has a single string parameter. As you can see, the parameters are declared almost exactly like variables. When we use procedures, the parameter name is not that important, but the parameter data type is. When we create procedures, parameters act like variables, so both the parameter name and data type are important.

Delphi has an enormous number of standard functions that you can use in your applications. Among these are two procedures that are really simple, extremely useful, and often used to optimize code: Inc and Dec. The Inc procedure can be used to increment any ordinal value by one or more, and the Dec procedure can be used to decrement any ordinal value by one or more. Their headers are:

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

Both procedure headers are a bit complicated to read at first, but the only thing that you have to know right now is that Delphi documentation uses brackets to show which parameters are optional. This means that both Inc and Dec can be called with one or two parameters.

Listing 5-1: Inc and Dec procedures

image from book
program Project1; {$APPTYPE CONSOLE} uses   SysUtils; var   x: Integer;   c: Char; begin   x := 10;   Inc(x); { faster way of writing x := x + 1; }   Dec(x); { faster way of writing x := x - 1; }   Inc(x, 5); { faster way of writing x := x + 5; }   Write(x);   c := 'a';   Inc(c);   Write(c); { c :=  'b'; }   ReadLn; end.
image from book

Not only is it faster to write Inc(x) than x := x + 1, theIncprocedure also executes faster. It is recommended that you use the Inc and Dec procedures whenever you can.

If you take a closer look at the code in Listing 5-1, you will notice that you can use the Inc procedure to increment a character value and that there is no comment that shows how to do this manually. If you wanted to increment a character value manually, you would have to write something like this:

var   c: Char; begin   c := 'a';   c := Chr(Integer(c) + 1); end.

This code increments a character value by performing two separate typecasts, one explicit and one implicit. It first converts the character to an integer, increments it by one, and then converts it back to a character value. Note that this code shouldn't be used in an application since there are much better ways of working with characters. You can read more about this and typecasting later in this chapter.



Inside Delphi 2006
Inside Delphi 2006 (Wordware Delphi Developers Library)
ISBN: 1598220039
EAN: 2147483647
Year: 2004
Pages: 212
Authors: Ivan Hladni

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net