Passing the vector as a parameter

when we are passing the vector as a parameter in the factorize function, why are we passing it by reference by using &

also is there a possibility to pass the function of the prime sieve itself as a parameter in the factorize function as we have done earlier when we passed the compare function

@vector.9974, we are passing by reference because passing by value is time inefficient , means if you pass a vector by value then it will take o(n) time to make a copy at every function call which is quite inefficient , this is why we pass vector by reference .

Let me show you by an example how,

So you have the given function as :-

int sum=0;`

void calcSum(vector<int> v,int i){  // call by value
    if(i==v.size()){
        return ;
    }
    sum+=v[i];
    calcSum(v,i+1);
    return ;
}

you might think that the complexity of function is o(n) . but wait…
you will notice that here function here is called by value
so every time you call the function then our original vector will be copied by the function and u know that copying a vector take O(n) so calling our function n times with subsequent copies will give us over all time complexity as O(n^2)
have the given function been like

int sum=0;

void calcSum(vector<int> &v,int i){ // call by reference 
    if(i==v.size()){
        return ;
    }
    sum+=v[i];
    calcSum(v,i+1);
    return ;
}

the time complexity would have been O(n), since reference does’nt require copying whole vector

In case of any doubt feel free to ask :slight_smile:
If you got your answer mark the doubt as RESOLVED

yes you can pass the seive vector as a parameter

pls share the syntax on how to pass the sieve vector as a parameter in the factorize function

the syntax would be simple , you just have to make the seive using vector
say, vector seive;
and pass it to any function you are using by reference
eg:-

int fun(vector&seive){

}

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.