Jump to content

Procedural parameter

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Jorge Stolfi (talk | contribs) at 17:37, 4 February 2006 (Created stub article). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

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 procedures, like pascal and more so when function closures are available. The same functionality (and more) is provided by objects in object oriented programming languages, but at a significantly higher cost.

Example

A typical example of procedural parameter use is the following implementation of the insertion sort algorithm, that takes two integer parameters a,b and two procedural parameters prec, swap:

isort(a, b, prec, swap)
  integer i, j;
  i  a;
  while i \leq b do
    j  i
    while j Failed to parse (syntax error): {\displaystyle >}
 a and prec(j, j−1) do
      swap(j, j−1); j  j−1
    i  i+1

This procedure can be used to sort the elements x[a] through x[b] 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 the data stored in x[r] should come before the data stored in x[s], according to the client's needs. The swap function should exchange the contents of x[r] and x[s], and return no result.