C++ Builder Tutorials

C++ Tutorial: passing arguments by Value / Reference

In C++ arguments can be passed to a function by value or by reference.


By Value

This is the default way for passing arguments to a function.
The argument's value is copied into the function's parameter.
The original argument can not be modified by the function.

Example:

String S = "abc";
String S2;
S2 = SomeFunction(S);

By Reference

When an argument is passed by reference, you are passing the address of the argument instead of its value. The function can modify the original argument.

To pass a variable by reference, insert an ampersand & before it, such as for the function DecodeDate that places values in its arguments Year, Month and Day:

TDateTime Date1 = Date;
unsigned short Year, Month, Day;
Date1->DecodeDate(&Year, &Month, &Day); 
Some types of variables are always passed by reference, without the need of an ampersand. This is the case for arrays, objects and structures.
See also:

  Functions in C++