Creating Variables in VEXcode Pro V5

The VEX Visual Studio Code Extension has replaced VEXcode Pro V5, which is now end-of-life.

VEXcode Blocks and VEXcode Text remain actively developed and supported for all VEX platforms.

What are Variables?

Variables are like containers that store something. Each container “type” possesses a set of properties including size, and what kind of content it can hold.

Two attributes that you should know:

Types

Each variable must have a “type.” This article will cover only the primitive types - char, short, int, long, long long, float, and double.

The primitive data type can hold only scalar values, no dimension to it. Choosing the right type is critical as the type affects how the variable is stored.

Declarations

Variables must be declared/created before you can use it.

e.g.

int height;
float amount;

Initialization means pre-populating the variable with an initial value:

e.g.

height = 5.0;
amount = 0.0;

Or, you can do both declaration and initialization together:

int height = 5.0;
float amount = 0.0;

Sample:

int length=5, width=10;
Brain.Screen.print("Perimeter of a %d by %d rectangle = %d",
length, width, (length + width)*2 );

Sample to show issues with something not initialized:

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  int length=5;
  int width;
  int perimeter = (length + width) * 2; //Incorrect, as width is not initialized

}

Sample to show invalid declaration:

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  int length=5, width;
  int length; //Incorrect. You cannot declare with an existing variable name.
  int x, float y; //Incorrect. You must use ";", as int and float are different types.
  int perimeter = (length + width)*2; //Incorrect, as width is not initialized

}

For more information, help, and tips, check out the many resources at VEX Professional Development Plus

Last Updated: