Value parameters

from Wikipedia, the free encyclopedia

Value parameters ( call by value or pass by value ) are parameters of subroutines that save copies of the arguments passed when the call is made. In addition to value parameters, programming languages often also provide reference parameters that only form another name (alias) for the arguments passed and can allow them to be changed.

example

The C programming language always uses value parameters.

Function definition:

void unwirksamerTausch(int p1, int p2) {
    int tmp = p1;
    p1 = p2;
    p2 = tmp;
}

Function call:

 int a1 = 1; int a2 = 2;
 unwirksamerTausch(a1, a2);
 printf("%i, %i", a1, a2);

Output:

1, 2

The values ​​of the variables a1 and a2 are copied to the ineffective exchange function in its parameters p1 and p2 and only exchanged within these.

In C, fields are always created as a pointer and this, not the field, is passed as a value parameter. Pointers are also passed as value parameters, so pointer arguments are copied into the function parameters. However, due to the pointer nature, these parameters can be used to change the data not belonging to the function. This is not the same as a call by reference , but offers similar possibilities.

implementation

If an assignment is made to a value parameter within the subroutine, a compiler must generate a copy of the parameter when the subroutine is called . This requires both disk space and execution time. With larger data structures such as B. large fields, this can be problematic.

Optimizing compilers can automatically determine whether such a copy is necessary and, if necessary, dispense with it.

Programming languages ​​used (excerpt)

See also