Passing a vector to a function in C++

So in this post, we will learn two different ways in which a vector can be passed to a function. They are namely: Pass by Value and Pass by Reference. We will also see how passing by value doesn’t actually modify the original array while passing by reference does modify the same.

Pass by Value

In this method of passing the vector, we simply pass the vector to the function as one of its parameters. What the function does is that it creates a copy of the original function and then uses the copy inside itself. All the modifications done will alter the copy vector only, and the original vector will remain untouched. Here is a sample code to understand pass by value :

#include<bits/stdc++.h>
using namespace std;

void increment(vector<int> array){
    int n = array.size();
    for(int i = 0; i<n; i++){
        array[i]++;
    }
    return;
}

int main(){
    vector<int> array = {1,2,3,4,5,6};
    increment(array);
    int n = array.size();
    for(int i = 0; i<n; i++){
        cout<<array[i]<<" ";
    }
    return 0;
}

Here, the function ‘increment‘ increases the value of every element of the vector ‘array by 1. But when we invoke the function ‘increment‘ inside the ‘main()‘ function by passing the vector ‘array as the parameter, and then when we print the elements of the vector, they remain the same. This is because the vector is passed by value to the function. So the function creates a copy of the vector inside itself instead of using the original vector itself, and does all the modifications on the copy vector and the original vector remains intact. So, when we print the elements of the vector, they remain the same. The output of the code looks like :

1 2 3 4 5 6

Pass by Reference

In this method of passing the vector, we pass the original vector to the function as one of its parameters. The function performs all the operations on the original vector, and not on the copy vector. A vector can be passed by reference by using an ‘&’ symbol before the name of the vector. Here is a sample code to understand pass by value :

#include<bits/stdc++.h>
using namespace std;

void increment(vector<int>& array){
    int n = array.size();
    for(int i = 0; i<n; i++){
        array[i]++;
    }
    return;
}

int main(){
    vector<int> array = {1,2,3,4,5,6};
    increment(array);
    int n = array.size();
    for(int i = 0; i<n; i++){
        cout<<array[i]<<" ";
    }
    return 0;
}

Notice how the ‘&’ is placed before the name of the array during the function definition. This time, the original vector gets modified, and each value gets incremented by one. The output of the above code is :

2 3 4 5 6 7

Advantages and Disadvantages

Call by reference saves a lot of time during code execution by not creating a copy of the function arguments every time the function is called. On the other hand, if the original data is sensitive and tampering must be avoided, then call by value is a better option. So depending on the use, both methods can prove to be useful.

That was all for this post, hope you guys understood. Feel free to leave comments down below in case of any doubts/queries.

Leave a Reply

Your email address will not be published. Required fields are marked *