Procedures permit you to pass both addresses of variables and values from one routine to another. For example, you can calculate a new value using others which have been passed using value parameters. The new value can be passed back to the calling routine using a variable parameter. However, you are not allowed to use a procedure call directly in an expression. Consider the following, which uses the procedure squareproc to calculate y for the function y = x2:
var
x, y : integer;
procedure squareproc ( x : integer; var xsquared : integer);
begin
xsquared := x * x
end;
{ from main line }
for x := 1 to 5 do
begin
squareproc ( x, y );
writeln( x:5, y:5 )
end
It would be nice if we could have just one statement inside the for-to-do loop. This is not possible using a procedure to calculate the value of y, because the procedure call cannot be used as a parameter in another procedure call.
There is another type of Pascal subprogram called a function which is used for this purpose.
A function is declared as being of a specific data type. Consider the following function heading:
function squarefunct (x : integer) : integer;
which states that the function requires one integer parameter and calculates an integer value. Note the colon between the parameter list and the type specification for the function.
To illustrate how functions can be defined, three different definitions for squarefunct are given below. Each of these versions demonstrates something about how functions can be defined.
function squarefunct (x : integer) : integer; var squarelocal : integer; begin squarelocal := x * x; squarefunct := squarelocal end;
This first version calculates the square of the value passed as a parameter and assigns the calculated value to the local variable squarelocal. This definition allows you to examine the calculated value as you trace through the function with the debugger.
function squarefunct (x : integer) : integer; begin x := x * x; squarefunct := x end;
This second version uses the parameter as the temporary holder for the calculated value. Remember, since the parameter is a value parameter, the value of the actual parameter in the calling routine does not change.
function squarefunct (x : integer) : integer; begin squarefunct := x * x end;
The final version assigns the calculated value to the function identifier immediately. For this simple task, this version is obviously the most efficient.
Note that in each definition the last statement which is executed before the function is exited assigns the calculated value to the function identifier. The Pascal language was designed in this way so that you would understand how the function call can be used in an expression or as a parameter for another function or procedure call.
The following variable declarations and code fragment show how any of the above versions of squarefunct could be used in a manner similar to that used for the procedure squareproc in the code discussed above.
var
x, y : integer;
{ from main line }
for x := 1 to 5 do
begin
y := squarefunct(x);
writeln( x:5, y:5 )
end
The following variable declaration and code fragment show how any of the above versions of squarefunct could be called as a parameter for writeln.
var
x : integer;
{ from main line }
for x := 1 to 5 do
begin
writeln( x:5, squarefunct(x):5 )
end
© 2000 DFStermole
Created: 26 Apr 00