I am learning C++ and confused why a changes occurred in function gets reflected in main. Can anyone say how to change the array in function without reflecting in main module..
thx for the help
#include <iostream>
using namespace std;
void function1(int x[],int n){
for(int i=0;i<n;[enter image description here](https://i.stack.imgur.com/G17A6.png)i++){
x[i]=0;
}
}
int main(){
int n;
cout<<"Enter the number of terms :";
cin>>n;
int a[n];
for(int i=0;i<n;i++){
cin >> a[i];
}
function1(a,n);
//after calling a function
//printing an array declared in main
for(int i=0;i<n;i++){
cout<<a[i]<<endl;
}
return 0;
}
I wrote a code to check the changes of data in main and function module. After calling the function, i thought the data entered to the array before calling the function wouldn't be changed. However, changes in data in function, reflected in main module