The Delphi Language, Part 2
(Page 1 of 14 )
In this second half of the chapter (
Delphi for .NET Developer's Guide, Sams, ISBN: 0-672-32443-1, 2004), author Xavier Pacheco talks about how Delphi enables you to pass parameters by value or by reference to functions and procedures, provides an explanation of units, using Delphi objects and more. (Here's the
link to part 1 for reference.)
Procedures and Functions
As a programmer, you should already be familiar with the basics of procedures and functions. A procedure is a discrete program part that performs some particular task when it's called and then returns to the calling part of your code. A function works the same except that a function returns a value after its exit to the calling part of the program.
Listing 5.1 demonstrates a short Delphi program with a procedure and a function.
Listing 5.1 An Example of a Function and a Procedure
1: program FuncProc;
2:
3: {$APPTYPE CONSOLE}
4:
5: procedure BiggerThanTen(I: Integer);
6: { writes something to the screen if I is greater than 10 }
7: begin
8: if I > 10 then
9: writeln('Funky.');
10: end;
11:
12: function IsPositive(I: Integer): Boolean;
13: { Returns True if I is 0 or positive, False if I is negative }
14: begin
15: Result := I >= 0;
16: end;
17:
18: var
19: Num: Integer;
20: begin
21: Num := 23;
22: BiggerThanTen(Num);
23: if IsPositive(Num) then
24: writeln(Num, ' Is positive.')
25: else
26: writeln(Num, ' Is negative.');
27: end.
Result - The local variable Result in the IsPositive() function deserves special attention. Every Delphi function has an implicit local variable called Result that contains the return value of the function. Note that unlike C#'s return statement, the function doesn't terminate as soon as a value is assigned to Result.
If you want to duplicate the behavior of C#'s return statement, you can call Exit immediately after assigning to Result. The Exit statement immediately exits the current routine.
You also can return a value from a function by assigning the name of a function to a value inside the function's code. This is standard Delphi syntax and a holdover from previous versions of Borland Pascal. If you choose to use the function name within the body, be careful to note that there is a huge difference between using the function name on the left side of an assignment operator and using it somewhere else in your code. If on the left, you are assigning the function return value. If somewhere else in your code, you are calling the function recursively!
Note that the implicit Result variable isn't allowed when the compiler's Extended Syntax option is disabled in the Project, Options, Compiler dialog box or when you're using the {$EXTENDEDSYNTAX OFF} (or {$X-}) directive.
Passing Parameters Delphi enables you to pass parameters by value or by reference to functions and procedures. The parameters you pass can be of any basic or user-defined type or an open array. (Open arrays are discussed later in this chapter.) Parameters also can be constant if their values will not change in the procedure or function.
Value Parameters Value parameters are the default mode of parameter passing. When a parameter is passed by value, it means that a local copy of that variable is created, and the function or procedure operates on the copy. Consider the following example:
procedure Foo(I: Integer); When you call a procedure in this way, a copy of Integer I will be made, and Foo() will operate on the local copy of I. This means that you can change the value of I without having any effect on the variable passed into Foo().
Reference Parameters Delphi enables you to pass variables to functions and procedures by reference; parameters passed by reference are also called variable parameters. Passing by reference means that the function or procedure receiving the variable can modify the value of that variable. To pass a variable by reference, use the keyword var in the procedure's or function's parameter list:
procedure ChangeMe(var x: longint);
begin
x := 2; { x is now changed in the calling procedure }
end;
Instead of making a copy of x, the var keyword causes the address of the parameter to be copied so that its value can be directly modified.
Using var parameters is equivalent to passing variables by reference in C# using the ref keyword or in Visual Basic .NET using the ByRef directive.
This chapter is from Delphi for .NET Developer's Guide, by Xavier Pacheco (Sams, 2004, ISBN: 0-672-32443-1). Check it out at your favorite bookstore today.
Buy this book now. |
Next: Out Parameters, Constant Parameters, and Open Array Parameters >>
More .NET Articles
More By Xavier Pacheco