Skip to main content

Section 13.7 Functions and Vectors

Vectors can be the parameters or the return types for functions. For now, we need to specify what type of vector the function accepts or returns (vector<int>, vector<string>, etc...). Later on we will learn how to make a β€œgeneric” function that can work with a vector that stores any type.
Say we want to write a function that can add up all of the values in a vector<int.. It could look like:
// total up all the values in numbers
int total(vector<int> numbers) {
    int total = 0;
    for (int x : numbers) {
        total += x;
    }
    return total;
}
However, that version of the function would pass by value and copy the entire vector that was passed. It would be better to pass by const reference. Nothing other than the parameter needs to be changed:
// total up all the values in numbers
int total(const vector<int>& numbers) {
    int total = 0;
    for (int x : numbers) {
        total += x;
    }
    return total;
}
To return a vector, we can declare the return type to be a vector<T> and then make sure to return an appropriate type of vector. (A returned value is always copied, though the compiler may do some tricks to avoid actually copying the data). This function returns a new vector that is a copy of the one passed in which every element gets doubled:
Listing 13.7.1.
Note that we could write the function so that instead of making a new vector and returning that we modified the vector that was passed in:
Listing 13.7.2.
In this version, we must take the vector parameter as a reference that is not const so we can modify the vector (line 7). Similarly, as we use a range-based loop to traverse the elements, we need to declare the element type to be an int& - a reference to an int - so that we are working with the actual element and not a copy of its value (line 9). Because the parameter is modified, there is nothing new to return from the function. So the return type is declared as void (line 7) and when it is called in main, there is no result to store (line 17). Because we passed by reference, all of the changes made to numbers in doubleValues really were made to myList that was passed in from main.

Checkpoint 13.7.1.

Construct the makeEven function that makes a new vector by looping through vec to copy elements. Any element with an odd value get 1 added to make it even before it is copied.

Checkpoint 13.7.2.

Suppose the following code is run:
vector<string> lauren = {"happy", "to", "you", "September", "birthday", "girl"}
How would you save the string "birthday" from lauren to the variable nurse?
  • nurse = lauren.at(4)
  • Vectors are zero-indexed, so the fifth element is the fourth index.
  • nurse = lauren.at(5)
  • Remember, vectors are zero-indexed!
  • nurse = lauren.at(6)
  • Remember, vectors are zero-indexed!
  • nurse = lauren(4)
  • This is not proper vector indexing.
  • nurse = lauren(5)
  • This is not proper vector indexing. Also, vectors are zero-indexed.
You have attempted of activities on this page.