Name parameters

from Wikipedia, the free encyclopedia

A name parameter (English call by name ) is a parameter of a subroutine in programming languages that is not calculated when it is transferred, but only when it is used, according to the signature of the actual parameter. This was mainly used in the COBOL programming language and also in ALGOL 60 , but is unusual in modern languages.

To classify: In Fortran there are only reference parameters , in C and Java only value parameters , in C ++ there are both. With the exception of preprocessor macros in C and C ++, whose arguments are name parameters, name parameters do not exist in any of these languages.

Name parameters enable both the transfer and return of values.

A simple example

A[1] := 10
A[2] := 20
i := 1
x := Funkt (A[i], i)

FUNCTION Funkt (a, i) : Real
  i := i+1
  RETURN a
END

x is assigned the value 20. In the case of value parameters, however, x would be 10, as is the case with reference parameters.

An applied example in ALGOL 60

The Simpson approximation formula for calculating the integral is used as an example . An area integral


can be divided into n strips of width h . Then the approximation used here is:

In Algol 60 this is implemented as follows (the example is not optimal, but a precise implementation of the definition):


 1 'REAL' 'PROCEDURE' SIMPSON(A,B,F,X,N);
 2    'REAL' A, B, X;
 3    'INTEGER' N;
 4    'REAL' 'PROCEDURE' F;
 5 'BEGIN'
 6    'INTEGER' I;
 7    'REAL' H, S1, S2;
 8    'ARRAY' Y[0:N]; 
 9    H := (B-A)/N;
10    'FOR' I:=0 'STEP' 1 'UNTIL' N 'DO' 
11       'BEGIN'  
12          X:=A+H*I;
13          Y[I]:=F;
14       'END';
15    S1 := S2 := 0;
16    'FOR' I:=1 'STEP' 2 'UNTIL' N-1 'DO' S1:=S1+Y[I];
17    'FOR' I:=2 'STEP' 2 'UNTIL' N-2 'DO' S2:=S2+Y[I];
18    SIMPSON:=H/3*(Y[0]+Y[N]+4*S1+2*S2);
19 'END'

All parameters here are name parameters. Special use is made of this for parameters X and F (lines 12 and 13). A dynamic field is declared in line 8 . The integral

is calculated approximately ( n = 20 ) using

 Y:=SIMPSON(0,1,SIN(K),K,20);

In subprogram SIMPSON is here F by SIN (K) and X by K replaced.

Double integrals are also possible:

The definite integral

can approximate with

 Z:=SIMPSON(0,1,SIMPSON(-1,1,EXP(SQRT(X 'POWER' 2+Y 'POWER' 2)),Y,20),X,20);

be calculated. This cannot be implemented in this form with reference parameters, value parameters or with simple C macros.