Call by reference
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 the arguments passed as its parent program and change. Any change remains even after exiting the subroutine obtained since no copies are generated for the subroutine. The disadvantage here is that an unwanted influence of the main program variables in the subroutine is possible. The name comes from the fact that the compiler in most programming passes the address of the memory area of a variable or an array element (ie, a pointer to the variable or array element ), the reference ( reference, alias) can be considered.
Usually, in addition to reference parameters are also value parameters are available, rare value result parameter.
Example
In the Pascal language has the subroutine call for each reference parameter is a variable, an array or structure element are given as an actual parameter:
(* Transfer of the variable X as a reference parameter in PASCAL *) PROGRAM Demo ( input, output); PROCEDURE Increment (VAR N: Integer); BEGIN N: = N 1; END; VAR X: integer; BEGIN Write ( 'Please enter X '); ReadLn (X); Increment (x ); Write ( ' The successor of X is :'); WriteLn (X); END. Increment the function has the reference parameter N ( line 4), which is in line 13 is replaced by the variable X as an actual parameter X. The subroutines Write and WriteLn (lines 11 and 13) use value parameters, while ReadLn a reference parameter requires ( line 11), for here also X is employed. This is, for example WriteLn (2 * X) is easily possible while ReadLn ( X * 2) generates a syntax error in the translation.
Here is a small example in C :
# include
When you call the function, only the value value ( line 8) must be passed. The function then has squared the value and accepted value this value over the reference.
Formal and actual parameters
In the example, the reference parameter N ( keyword VAR ) is used, which is generated at the declaration of the subroutine. If VAR is omitted, a value parameter is generated. When you call the actual parameter N is passed.
Modern ( optimizing ) compiler can determine if a copy is needed and, if appropriate, without it in passing value parameters.
Simulation of reference parameters by pointer
The following example is written in the language C, which has no reference parameters. Through the use of pointers, but a similar performance can be realized.
/ * Transfer of the variable x as a pointer parameter p in C * / # include increment_p void (int * p) { (* p) = (* p) 1; } int main () { int x; x = 3; increment_p ( & x); printf ( "The result is % d \ n", x); } On line 15, the address-of operator & is used so that the address of the variable x is passed to the function. This is passed to the Zeigerparameterp ( line 5).
Reference parameters in the form of references
In the language C reference parameters just as in C are implemented as pointers. It also a language extension has been specially introduced for this purpose. This language extension is called reference and has the following notation:
Void increment_r ( int & r) { r = r 1; } In comparison, once again the example of pointer:
Increment_p void (int * p) { (* p) = (* p) 1; } So increment_r In the variant accounts for the Zeigerdereferenzierungen in the function body.
Calling the function:
... increment_r (x); ... In contrast to the variant with increment_p is thus used here in the call not the address operator &.
Special case of Java
In the Java language, a parameter value is automatically used for primitive data types. With objects, the object reference is passed as a value parameter by copy. The called method can therefore change the state of the referenced object, but not the original reference itself - so it shows after returning from the method call to the same object as before. The terminology of many Java publications obscured this fact.
This shows the following example:
Import java.util.Date; public class Test { public static void main (String args [ ]) { Date s = new Date ( 100000000000l ); System.out.println (" main 1 :" s ); call ( s ); System.out.println (" main 2 :" s ); } public static void call ( s Date ) { System.out.println ( "call 1: " s ); s.setTime ( 200000000000l ); System.out.println ( "call 2: " s ); } } The output is:
Main 1: Sat Mar 03 10:46:40 CET 1973 call 1: Sat Mar 03 10:46:40 CET 1973 call 2: Mon May 03 20:33:20 CET 1976 main 2: Mon May 03 20:33:20 CET 1976 However, a modified reference is not returned. This is illustrated by the following example:
Import java.util.Date; public class Test { public static void main (String args [ ]) { Date s = new Date ( 100000000000l ); System.out.println (" main 1 :" s ); call ( s ); System.out.println (" main 2 :" s ); } public static void call ( s Date ) { System.out.println ( "call 1: " s ); s = new Date ( 200000000000l ); / / Here is the difference for the example above System.out.println ( "call 2: " s ); } } The output is:
Main 1: Sat Mar 03 10:46:40 CET 1973 call 1: Sat Mar 03 10:46:40 CET 1973 call 2: Mon May 03 20:33:20 CET 1976 main 2: Sat Mar 03 10:46:40 CET 1973 used programming languages
- Pascal
- Modula-2
- Fortran
- C
- Java
- PL / I
- PHP