Return to Topic Menu | Computer Science Main Page | | Terms of Use | Resource CD

Passing apvector Arguments
to Functions

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> &num); //prototype

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> &num);

  • 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 Mantra!!!):
    int main(void)
        apvector <int> num(20);
        . . . 
    //function expecting an address
        . . . 


ampersand (&)
"address of"  






Mantra:  "The name of the array is the address of the first element of the array."