Reference (computer science)

A reference is a reference to an object. A reference is now an alias for an object.

Example in C

Operation of a reference

Int nTo = 5; int & nreference = nTo; Created / / reference object                              / / The reference refers to the original   nreference = 20; / / Has the reference and thus also the original value of 20 to int ncopy = nTo; / / Ncopy = 20 Example of parameter passing by reference, see also reference parameters

Void square ( int & value) {      value = value * value; }   int main () {      nNumber int = 5;      square ( nNumber ); / / Call the function - then nNumber == 25      return 0; } It will work with the original variables, regardless of whether the variable name is identical in function with the name of the call variables.

Example of object references

Bank & NBank = Bank Directory :: nachBLZ ( " 76543210 "); / / A reference to a database object is acquired Account & nKonto1 nBank.kontoZugriff = ( " 1234567 "); / / A reference to a specific account object is acquired Account & nKonto2 nBank.kontoZugriff = ( " 1111111 "); / / A reference to another account object is acquired nKonto1.einzahlung (100.00, " EUR ", nKonto2 ); / / A method is called on nKonto1 The references NBank nKonto1 and nKonto2 each reference an object. The & symbol identifies these variables as references within the meaning of the language.

Example in Pascal

Surrender by reference (the value of the transferred variable is changed)

Procedure squaring (var value: integer); begin      value: = value * value; end; This procedure can only be passed a variable, not an expression. value is the local name for the supplied reference variables; with the assignment of a value to the content of the transferred variable is changed directly.

Transfer by value (ie only the value, not the variable itself, and the value of the transferred variable is not changed )

Function square (value: integer ): integer; begin      Result: = value * value; end; Even if an assignment would be carried at value, this would not change the content of an approximately passed variables: Pass only one value; the identifier value is a local variable that is only valid within the function.

  • Programming language element
675851
de