Vector pass by a function

So, recently I was tried to pass a vector as a parameter through input_Arr (v,n); function to take input in the vector but I’m unsuccessful to do that. So, can anyone clarify me that vector not works like arrays when it comes to pass by a function!!!

@Kinjal Vector is different from array and cannot be handled very similar to array.


Refer this. This might help.

If still your problem is not solved then please save your code on ide.codingblocks.com and share its link.

I have already gone through GeeksforGeeks. But my code still not working. Can you please give it try?

@Kinjal Change line 6 to void input_Arr(vector &v,int n)
Pass the vector as reference so that changes in vector in this function is reflected back in the main function.


But, it’s so weird way to pass the vector. I actually dont get it, how it works. What’s the difference between &v and v for vector v?

&v means that you are passing the vector by reference. v means you are passing by value.
When you are modifying the vector and want the modifications to be reflected back in the main then you usually pass by reference. Like in your function void input_Arr vector modification is done…so it is passed be reference using & operator.
But in max_subarray_sum function, actual vector is not being modified so no need to pass it by reference. You can simply pass it without using & operator.

Ooh…you mean that &v act as a reference vector(reference variable) inside called function input_Arr().
So that if we change in the reference vector then it’ll effect on the vector it’s pointing to. Thank you for sharing your wisdom, sensei.