Programing globals and variables easyc v4

Ok so it may sound noobish, partially because it might be. But I was curious on a what variables/globals are, how to program them and how to apply them. In the end I am wanting to have quad shaft encoders lift to one of three predesignated states with the press of a button(one for each level) any help would be greatly appreciated

Global variables are variables that can be accessed by any method/function within your program. Local variables are variables that can be accessed only by the function that creates them.

int x = 42; // global variable
void myFunc()
    int y = 20; // local variable
    x = 36; // valid statement
void myOtherFunc()
   y = 47; // trying to use a local variable from another function, invalid statement
   x = 36; // valid statement

When would you want to use global variables? If you want to be passing around variables from function to function. Note that you’d also need global variable if you want to retain from pass to pass.

int x = 42;
void myFunc() // calling myFunc() the first time will set x to 47, calling it again will doSomething() because x is 47
    if(x == 47)
       x = 47;

When would you want to use local variables? If you want to use a variable only within your function.

You should worry about global vs local variables because it keeps your code cleaner. Go with a local variable until you need it to be global.

Edit: It also helps prevent unwanted tampering with your variables (imagine a function that accidently changes something that’s needed by another). Not a big issue in “small” (I use small relatively) procedural (not object-oriented like Java, C++ or C#) programming.

A great example of a local variable that you have undoubtedly used is in a for loop.

However, as edjubuh stated, they do keep the code a lot cleaner,and you can recycle common names, like “rotations”, or “time” in sensor loops for autonomous, making code more understandable,without having to set the same global variable to 0 every single function.

I used to create large lists of variables at the beginning of every program, but now that I actually can code “well”, I use more and more local variables.

Honestly I can say that I have never(knowingly) used the loop. This is the third year for hesston robotics, we’re alittle behin yes I know, but our mascot is a swather. Hince the name, and just incase you were worndering its pretty much a combine like machine for farming.
But back to the point athand. I understand how to make local variables, but i have never been able to apply them. I tried last year for a multi jumper automus didnt work out for me. Ended up writing a really long complex code which I feel couldve been shortened greatly (not to mention with much less headache) with the use of variables
Now I understand that this is pseudocode but how would I converted into actual code in order to use. For example(and because I cannot for the life of me remember the buttons symbols on the ones with four buttons we’re just gonna use the compass north. South east west) on joystick 2 (would I have too put in something for that?) would I make an int varible for all the buttons like btnN bntS bntE an bntW? Then put them in an if then statment, so if bntW is pressed it raises up to xxxx time?

Im using this pseudocode that I “borrowed” from apalrd coment in they other forum

//Variables used overall
int setpoint,sensor,error,p,motor = 0;
/* HMI code section in operatorControl*/
if(btn1) setpoint = 1024; //This is a calibrated position
if(btn2) setpoint = 2048; //This is another calibrated position
if(btn3) setpoint = 4098; //This is a third calibrated position

/* Feedback control code section in its own function */
sensor = GetAnalogValue...
error = setpoint - sensor;
p = error * kp; //kp is a calibrated gain
motor = p;//Add the terms - There is only one term