I think guix gave you a good answer. If you make the variables global, everything in the source code file has access to those variables from the point of their definition to the end of the file. This complicates debugging because the bogus value could have happened anywhere in the file. If you use pointers, you can hide the data within a function (e.g., encapsulation) yet still allow some other function to change their values.
Consider this program:
void setup() {
int a = 5;
int b = 10;
int c = 20;
Serial.begin(9600);
Serial.print(" a = ");
Serial.print(a);
Serial.print(" b = ");
Serial.print(b);
Serial.print(" c = ");
Serial.println(c);
ChangeMultipleValues(&a, &b, &c);
Serial.print("a = ");
Serial.print(a);
Serial.print(" b = ");
Serial.print(b);
Serial.print(" c = ");
Serial.println(c);
}
void ChangeMultipleValues(int *x, int *y, int *z)
{
*x = *x + 10;
*y = *y + 10;
*z = *z + 10;
}
void loop() {
}
Every variable has an lvalue and an rvalue. The lvalue is the memory address where a variable "lives" in memory while the rvalue is what's stored there. In a regular function call where you want to pass a value in, as in:
int j;
j = 10;
myFunction(j);
Now suppose that j lives in memory at address 200, its lvalue. To assign the value 10, the compiler goes to j's lvalue and changes its rvalue to 10. Because Arduino int's take two bytes, the rvalue 10 must "fit" within those two bytes. When you call myFunction(), the compiler fetches j's lvalue, makes a copy of its rvalue (10), and passes it to the function. Note that the function has no clue of j's lvalue, only its rvalue.
Now change the last statement to:
myFunction(&j); // Pass the memory address of j--its lvalue--not a copy of its rvalue
Note the "address of" operator, &. This says to the compiler: "Instead of making a copy of the rvalue of j, send it's lvalue instead". So, the number 200 is sent to the function, not 10. In the function, the first line says:
void ChangeMultipleValues(int *x, int *y, int *z)
Instead of sending you copies of the variables, I'm giving you their addresses in memory (i.e., their lvalues). The statements within the function use the process called indirection to go to those memory addresses and permanently change the rvalues found there.
Because you sent the address of the variables to the function, the compiler needs to know its working with lvalues, not rvalue as is the norm. That what the indireciton operator (i.e., the '*') tells the compiler. So instead of using the rvalues of a, b, and c directly, it uses their lvalues to find where the "real" variables live in memory and then changes their values.
Besides the reference mentioned here, you can Google "pointers in C" and read much more.