Functions that take apvector
arguments are declared in the same way as functions that take
other kinds of arguments. The only difference is that when using an
apvector the argument in the prototype
and in the function header is preceded by the
& (ampersand) symbol. For example, consider this function:
int AddValues(apvector <int>
The ampersand tells the compiler that the
function will be working with the original array (vector), not with a
temporary copy. The use of the ampersand prevents unnecessary copying of
the array and allows the function to deal directly with the original array.
If your function will not be changing any elements in the array, it is possible to
guarantee that no changes occur by adding the word const.
Think of it as a "safety" lock to protect the array.
int AddValues(const apvector <int>
In most situations, the function will
need to know the size of the array it is accessing. By allowing the
function to determine the length of the array (rather than just stating the
number), the function will be written in a more generic form and can be
repeatedly used for arrays of varying sizes.
//function for adding
int AddValues(apvector <int> &num)
int len = num.length( );
. . .
Calling the function from main (remember the
apvector <int> num(20);
. . .
expecting an address
. . .