Procedural parameter


In computing, a procedural parameter is a parameter of a procedure that is itself a procedure.
This concept is an extremely powerful and versatile programming tool, because it allows programmers to modify certain steps of a library procedure in arbitrarily complicated ways, without having to understand or modify the code of that procedure.
This tool is particularly effective and convenient in languages that support local function definitions, such as Pascal and the modern GNU dialect of C. It is even more so when function closures are available. The same functionality is provided by objects in object oriented programming languages, but at a significantly higher cost.
Procedural parameters are somewhat related to the concepts of first-class function and anonymous function, but is distinct from them. These two concepts have more to do with how functions are defined, rather than how they are used.

Basic concept

In most languages that provide this feature, a procedural parameter f of a subroutine P can be called inside the body of P as if it were an ordinary procedure:
procedure P:
return f * f
When calling the subroutine P, one must give it one argument, that must be some previously defined function compatible with the way P uses its parameter f. For example, if we define
procedure plus:
return x + y
then we may call P, and the result will be plus * plus = * = 27. On the other hand, if we define
procedure quot:
return u/v
then the call P will return quot*quot = * = 4. Finally, if we define
procedure evil
return z + 100
then the call P will not make much sense, and may be flagged as an error.

Syntactic details

Some programming languages that have this feature may allow or require a complete type declaration for each procedural parameter f, including the number and type of its arguments, and the type of its result, if any. For example, in the C programming language the example above could be written as

int P


In principle, the actual function actf that is passed as argument when P is called must be type-compatible with the declared type of the procedure parameter f. This usually means that actf and f must return the same type of result, must have the same number of arguments, and corresponding arguments must have the same type. The names of the arguments need not be the same, however, as shown by the plus and quot examples above. However, some programming languages may be more restrictive or more liberal in this regard.

Scoping

In languages that allow procedural parameters, the scoping rules are usually defined in such a way that procedural parameters are executed in their native scope. More precisely, suppose that the function actf is passed as argument to P, as its procedural parameter f; and f is then called from inside the body of P. While actf is being executed, it sees the environment of its definition.
The implementation of these scoping rules is not trivial. By the time that actf is finally executed, the activation records where its environment variables live may be arbitrarily deep in the stack. This is the so-called downwards funarg problem.

Example: Generic insertion sort

The concept of procedural parameter is best explained by examples. A typical application is the following generic implementation of the insertion sort algorithm, that takes two integer parameters a,b and two procedural parameters prec, swap:
procedure isort:
integer i, j;
ia;
while ib do
ji;
while j > a and prec do
swap;
jj−1;
ii+1;
This procedure can be used to sort the elements x through x of some array x, of arbitrary type, in a user-specified order. The parameters prec and swap should be two functions, defined by the client, both taking two integers r, s between a and b. The prec function should return true if and only if the data stored in x should precede the data stored in x, in the ordering defined by the client. The swap function should exchange the contents of x and x, and return no result.
By the proper choice of the functions prec and swap, the same isort procedure can be used to reorder arrays of any data type, stored in any medium and organized in any data structure that provides indexed access to individual array elements.

Sorting floating-point numbers

For instance, we can sort an array z of 20 floating-point numbers, z through z in increasing order by calling isort, where the functions zprec and zswap are defined as
procedure zprec:
return ;
procedure zswap:
float t;
tz;
zz;
zt

Sorting rows of a matrix

For another example, let M be a matrix of integers with 10 rows and 20 columns, with indices starting at 1. The following code will rearrange the elements in each row so that all the even values come before all odd values:
integer i
procedure eoprec:
return < ;

procedure eoswap:
integer t;
tM;
MM;
Mt;

for i from 1 to 10 do
isort;
Note that the effects of eoprec and eoswap depend on the row number i, but the isort procedure does not need to know that.

Vector-sorting procedure

The following example uses isort to define a procedure vecsort that takes an integer n and an integer vector v with elements v through v and sorts them in either increasing or decreasing order, depending on whether a third parameter incr is true or false, respectively:
procedure vecsort:

procedure vprec:
if incr then
return v < v;
else
return v > v;

procedure vswap:
integer t;
tv;
vv;
vt

isort;
Note the use of nested function definitions to get a function vprec whose effect depends on the parameter incr passed to vecsort. In languages that do not allow nested function definitions, like standard C, obtaining this effect would require rather complicated and/or thread-unsafe code.

Example: merging two sequences

The following example illustrates the use of procedural parameters to process abstract data structures independently of their concrete implementation. The problem is to merge two ordered sequences of records into a single sorted sequence, where the nature of the records and the ordering criterion can be chosen by the client. The following implementation assumes only that each record can be referenced by a memory address, and there is a "null address" Λ that is not the address of any valid record. The client must provide the addresses A, B of the first records in each sequence, and functions prec, next, and append, to be described later.
procedure merge:
address ini, fin, t
ini ← Λ; fin ← Λ
while A ≠ Λ or B ≠ Λ do
if B = Λ or then
tnextA
fin ← appendA; if ini = Λ then inifin
At
else
tnextB
finappendB; if ini = Λ then inifin
Bt
return ini
The function prec should take the addresses r, s of two records, one from each sequence, and return true if the first record should come before the other in the output sequence. The function nextA should take the address of a record from the first sequence, and return the address of the next record in the same sequence, or Λ if there is none. The function appendA should append the first record from sequence A to the output sequence; its arguments are the address A of the record to be appended, and the address fin of the last record of the output list. The procedure appendA should return the updated address of the final element of the output list. The procedures nextB and appendB are analogous for the other input sequence.

Merging linked lists

To illustrate the use of the generic merge procedure, here is the code for merging two simple linked lists, starting with nodes at addresses R, S. Here we assume that each record x contains an integer field x.INFO and an address field x.NEXT that points to the next node; where the info fields are in increasing order in each list. The input lists are dismantled by the merge, and their nodes are used to build the output list.
procedure listmerge:
procedure prec:
return r.INFO < s.INFO
procedure next:
return x.NEXT
procedure append
if fin ≠ Λ then fin.NEXTx
x.NEXT ← Λ
return x

return merge

Merging vectors

The following code illustrates the independence of the generic merge procedure from the actual representation of the sequences. It merges the elements of two ordinary arrays U through U and V through V of floating-point numbers, in decreasing order. The input arrays are not modified, and the merged sequence of values is stored into a third vector W through W. As in the C programming language, we assume that the expression "&V" yields the address of variable V, "*p" yields the variable whose address is the value of p, and that "&" is equivalent to "& + i" for any array X and any integer i.
procedure arraymerge:

procedure prec:
return >

procedure nextU:
if x = & then return Λ else return x + 1

procedure nextV:
if x = & then return Λ else return x + 1

procedure append
if fin = Λ then fin ← &

return fin + 1

if m = 0 then U ← Λ
if n = 0 then V ← Λ
return merge

Example: Definite integral

Integrating over an interval

The following procedure computes the approximate integral f dx of a given real-valued function f over a given interval of the real line. The numerical method used is the trapezium rule with a given number n of steps; the real numbers are approximated by floating-point numbers.
procedure Intg:
float t, x, s; integer i
if b = a then return 0
xa; sf/2;
for i from 1 to n−1 do
ti/; x ← *a + t*b;
ss + f
sf/2
return *s/n

Integrating over a disk

Consider now the problem of integrating a given function g, with two arguments, over a disk D with given center and given radius R. This problem can be reduced to two nested single-variable integrals by the change of variables
The following code implements the right-hand-side formula:
procedure DiskIntg

procedure gring:

procedure gpolar:
float x, y
xxc + z*cos
yyc + z*sin
return g

integer mround
return z*Intg

return Intg
This code uses the integration procedure Intg in two levels. The outer level uses Intg to compute the integral of gring for z varying from 0 to R. The inner level defines gring as being the line integral of g over the circle with center and radius z.

History

Procedural parameters were invented before the age of electronic computers, by mathematician Alonzo Church, as part of his lambda calculus model of computation.
Procedural parameters as a programming language feature were introduced by ALGOL 60. In fact, ALGOL 60 had a powerful "call by name" parameter-passing mechanism that could simplify some uses of procedural parameters; see Jensen's Device.
Procedural parameters were an essential feature of the LISP programming language, which also introduced the concept of function closure or funarg. The C programming language allows function pointers to be passed as parameters, which accomplish the same end, and are often used as callbacks in event-driven programming and as error handlers. However, only a few modern C compilers allow nested function definitions, so that its other uses are relatively uncommon. Procedural parameters were provided also in Pascal, together with nested procedure definitions; however, since standard Pascal did not allow separate compilation, the feature was little used in that language, too.