Parameters


The best mechanism for passing user-defined values to functions and procedures is the parameter list. It is also possible to use global variables in procedures, but they should be avoided as much as possible.

There are several parameter types in Delphi: value, variable, constant, and out.

Value parameters are declared like this:

procedure ProcedureName(ParamName: DataType);

When you call a procedure and pass a value as the value parameter, the procedure receives only a copy of the original value.

Listing 5-9: Value parameter example

image from book
program Project1; {$APPTYPE CONSOLE} uses   SysUtils; procedure MakeAbsolute(Num: Integer); begin   if Num < 0 then     Num := -Num;   WriteLn('Absolute value = ', Num); end; var   x: Integer; begin   MakeAbsolute(10);   x := -100;   MakeAbsolute(x);   ReadLn; end.
image from book

Take a look at these lines of code:

  x := -100;   MakeAbsolute(x); { the value of x is still -100 }

When you call the MakeAbsolute procedure, the procedure creates the Num parameter and copies the value from the x variable to the Num parameter. The procedure changes the value of the Num parameter, but that change never affects the x variable because the procedure actually doesn't know that you passed a variable as the parameter. Because parameters act like local variables, changes made to the parameters are lost when the procedure finishes.

Variable, Constant, and Out Parameters

If you want to change the value of a variable in the body of the procedure, you have to use a variable parameter. Variable parameters are declared with the reserved word var:

procedure ProcedureName(var VarParam: DataType);

Value parameters are passed by value, while variable parameters are passed by reference. The difference is that value parameters receive a copy of the value, and variable parameters receive the address of the variable. When you use variable parameters, you work directly with original variables, not their copies. You can only pass variables as variable parameters. If you try to pass a constant value as a variable parameter, you will receive a compiler error.

Listing 5-10: Variable parameter example

image from book
program Project1; {$APPTYPE CONSOLE} uses   SysUtils; procedure MakeAbsolute(var Num: Integer); begin   if Num < 0 then     Num := -Num;   WriteLn('Absolute value = ', Num); end; var   x: Integer; begin //   MakeAbsolute(10); { Error. Constant value. }   x := -100;   MakeAbsolute(x);     { OK because x is a variable. }   WriteLn(x);          { x = 100 }   ReadLn; end.
image from book

Constant parameters are similar to value parameters, but they don't allow you to change the value of the parameter in the body of a procedure or function. Constant parameters are declared with the reserved word const:

procedure ProcedureName(const ConstParam: DataType);

Constant parameters are extremely useful when you're passing string values to a procedure or a function. You should always pass string values as constant or variable parameters. If you need to change the string value, use a variable parameter; otherwise, use a constant parameter.

String values should never be passed as value parameters because strings can be very large and constantly creating string copies at every procedure call would decrease application performance.

procedure SayHello(const UserName: string); begin   WriteLn('Hello, ', UserName); end;

Out parameters are very similar to variable parameters because they are also passed by reference. Out parameters are used to return values from the procedure or function to the caller. Like variable parameters, out parameters only accept variables.

procedure GetMeaningOfLife(out Number: Integer); begin   { According to Douglas Adams. }   Number := 42; end;

Multiple Parameters

If you want to pass more than one parameter to the procedure or function, you have to separate the parameters with semicolons.

procedure WriteLnEx(const AText: string; ACount: Integer); var   i: Integer; begin   for i := 1 to ACount do     WriteLn(i, '. ', AText); end;

If two or more parameters are of the same data type, they can be declared together and separated with a comma.

function Max(X, Y: Integer): Integer; begin   if X > Y  then     Result := X   else     Result := Y; end;

Default Parameters

Default parameters in Delphi are value or const parameters that have a default value. For instance, let's take a look at the following procedure.

procedure WriteLnEx(const AText: string; ACount: Integer); var   i: Integer; begin   for i := 1 to ACount do     WriteLn(i, '. ', AText); end;

The WriteLnEx procedure is able to output a string value as many times as you pass in the ACount parameter. But, if you need to output the string value twice, you have to pass 2 in the ACount parameter:

WriteLnEx('Hello', 2);  WriteLnEx('Something else', 2);

Constantly passing 2 in ACount soon becomes both tiresome and unnecessary. This is where default parameters can be used. By specifying a default value in the procedure or function declaration, you won't have to pass the value in the procedure call. The default parameter syntax looks like this:

procedure ProcedureName(Param: DataType = Value);

Listing 5-11 shows the updated version of the WriteLnEx procedure where the ACount parameter is declared as default.

Listing 5-11: Default parameter example

image from book
program Project1; {$APPTYPE CONSOLE} uses   SysUtils; procedure WriteLnEx(const AText: string; ACount: Integer = 2); var   i: Integer; begin   for i := 1 to ACount do     WriteLn(i, '. ', AText); end; begin   WriteLnEx('Hello');          { ACount = 2 }   WriteLnEx('Hello again', 5); { ACount = 5 }   ReadLn; end.
image from book

The only thing that changes when you declare a default parameter is the way you call the procedure or function. Note that you can now call the WriteLnEx procedure without passing a value in the ACount parameter.

WriteLnEx('Hello');

In this case, the compiler uses the default value specified in the procedure header, and the call to the WriteLnEx procedure actually looks like this:

WriteLnEx('Hello', 2);

To successfully use a procedure or a function in your code, you have to know what parameters the procedure or function accepts. If you need to take a look at the parameter list of a procedure or function, type the procedure or function name in the Code Editor, open the left parenthesis, and press Ctrl+Shift+ Space. The Code Insight feature of the Code Editor will display the parameter list (see Figure 5-6). Note that the hint displays the value of the default parameter and the entire default parameter is enclosed in brackets.

image from book
Figure 5-6: Code Insight parameter information

Default parameters must be declared at the end of the parameter list. For instance, the following procedure will not compile because default parameters cannot precede standard parameters. If you declare parameters after a default parameter, they also have to be default.

procedure CannotCompile(ADefault: Integer = 5; AValue: Double); { Error } begin end;



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