Jensen's Device is a computer programming technique that exploits call by name. It was devised by Danish computer scientist Jørn Jensen, who worked with Peter Naur at Regnecentralen. They worked on the GIER Algol compiler, one of the earliest correct implementations of ALGOL 60. ALGOL 60 used call by name.
Jensen's device exploits call by name and side-effects. Call by name is an argument passing convention that delays the evaluation of an argument until it is actually used in the procedure (a consequence of the copy rule for procedures). Algol introduced call by name.
A classic example of Jensen's device is a procedure that computes the sum of a series, :
In the procedure, the index variable k
and summation term ak
are passed by name. Call by name enables the procedure to change the value of the index variable during execution of the for
loop. Call by name also causes the ak
argument to be reevaluated during each iteration of the loop. Typically, ak
will depend upon the changing (side-effected) k
.
For example, code to compute the first 100 terms of a real array V[]
would be:
During the execution of Sum
, the actual argument i
will increment during each step of the for
loop, and each of the procedure's evaluations of ak
will use the current value of i
to access the successive array elements V[i]
.
Jensen's device is general. A double summation can be done as: