Reference parameters

from Wikipedia, the free encyclopedia

Reference parameters ( English call by reference or pass by reference ) are parameters of subroutines in programming languages , by means of which a subroutine can use and change the passed arguments like its superordinate program. Any change is retained even after exiting the subroutine, as no copies are made for the subroutine. The disadvantage here is that it is possible that main program variables in the subroutine can be inadvertently influenced . The name comes from the fact that in most programming languages ​​the compiler transfers the address of the memory area of ​​a variable or an array element (i.e. a pointer to the variable or the array element), which can be interpreted as a reference (reference, alias).

Normally, in addition to reference parameters, value parameters are also available, less often value result parameters .

example

In the Pascal language , a variable , field or structure element must be specified as an actual parameter for each reference parameter when the subroutine is called:

// Uebergabe der Variablen X als Referenzparameter in PASCAL
program Demo(input, output);

procedure Inkrementiere(var n: Integer);
begin
    n := n + 1; 
end;

var X: integer;
begin 
    Write('Bitte X eingeben');
    ReadLn(X);
    Inkrementiere(X);
    Write('Der Nachfolger von X ist: ');
    WriteLn(X);
end.

The function Inkrementierehas the reference parameter N(line 4), which is replaced in line 13 by the variable Xas the actual parameter X. The subroutines Writeand WriteLn(lines 11 and 13) use value parameters , while ReadLna reference parameter is required (line 11), which is also Xused here . This z. B. WriteLn(2*X)easily possible while ReadLn(2*X)a syntax error is generated during translation.

Here is a small example in C ++:

#include <iostream>

void do_the_square(double& x) {
    x = x * x;
}

int main() {
    double value = 2;
    do_the_square(value);

    std::cout << "The square_meters are: " << value << std::endl;

    return 0;
}

The function also do_the_square()works with a reference here, and since a reference is only a reference to a variable and so no value has to be returned, it is sufficient to set a reference here in the head of the function.

When calling the function, only the value value(line 8) has to be transferred. The function then squared the value and accepted valuethat value via the reference.

Formal and actual parameters

In the example, the reference parameter N(keyword VAR) is used that is generated when the subroutine is declared. If it is VARomitted, a value parameter is generated. The actual parameter is Ntransferred when it is called .

Reference parameters Value parameters
formal parameters simple variables and structured variables simple variables and structured variables
actual parameters only variables, fields, field elements, structure elements with an exactly matching data type; no constants and expressions any expressions such as 1.0 , 2 * X , sin (x) or type conversions
handing over Transfer as address (little effort for fields) As a copy (high effort for fields)
Assignment within the subroutine possible possible or forbidden (depending on the programming language)
Return of the value at the end of the subroutine Yes No

When passing value parameters, modern (optimizing) compilers can determine whether a copy is necessary and, if necessary, dispense with it.

Simulation of reference parameters using pointers

The following example is written in the C language, which has no reference parameters. A similar behavior can be realized by using pointers .

// Uebergabe der Variablen x als Zeigerparameter p in C

#include <stdio.h>

void increment_p(int* p) {
    *p += 1;
}

int main() {
    int x = 3;

    increment_p(&x);
    printf("Das Ergebnis ist %d\n", x);

    return 0;
}

The address operator is &used in line 15 so that the address of the variable is transferred xto the function. This is passed to the pointer parameter p(line 5).

Reference parameters in the form of references

In the C ++ language , as in C, reference parameters can be implemented as pointers. However, a language extension was also introduced specifically for this purpose. This language extension is called a reference and has the following notation:

void increment_r(int& r) {
    r += 1;
}

In comparison, the example for pointers again:

void increment_p(int* p) {
    *p += 1;
}

With the variant increment_r, the pointer referencing in the function body is omitted.

Calling the function:

// ...
increment_r(x); 
// ...

In contrast to the variant with increment_p, the address operator & is not used here when calling .

Programming languages ​​used

See also