Saltar al contenido

Lesson 80: By value or by reference

English

Copies or the real thing?

  • By value: the function gets a copy of the variable. If it changes the copy, the caller's variable stays the same.
  • By reference: the function gets the variable itself (C++ writes & before the parameter name). A change stays after the function ends.
Pseudocode (9618) C++
BYVAL X : INTEGER int x
BYREF X : INTEGER int &x
  • Use by reference when the function should change the caller's variables, for example a swap. Otherwise use by value and return the answer.

Iniciar sesión o crear cuenta

IGCSE, A-Level & AP