When writing large programs, functions are often defined to make the main function concise and clear. However, we must detail the impact of such a function.
Test procedure
#include <iostream>
using namespace std;
int a = 2;
void func(int n, int& m)
{
a = 3;
n++;
m++;
}
int main()
{
int b = 1;
int c = 5;
func(b, c);
cout << a << " " << b << " " << c << endl;
return 0;
}
Output result
in conclusion
- If the global variable is changed in the function, it really changes
- Local variables are just equivalent to assigning a value. If they change in the function, they will no longer be valid.
- Unless it is a reference, this will have an impact. (And arrays cannot have references, which is very annoying. Moreover, arrays can neither be judged equal, nor can an array be assigned to another array, which is really annoying. So I like to use vector in comparison)