Variables#
Introduction#
Variables store data and allow you to reuse and manipulate it throughout your program. C++ is a statically typed language, meaning you must declare the type of a variable explicitly when you create it. The type cannot be changed after declaration. For example:
int angle = 90; // angle is an integer
char dist[] = "Distance"; // dist is an array of characters
double steps = 2.5; // steps is a double
C++ is also strongly typed, which means you cannot perform operations on incompatible types without explicitly converting them. For example:
int blocks = 2; // blocks is an integer
char rings[] = "4"; // rings is a string
int result = blocks + rings; // Creates a compilation error
This API explains common variable types in C++. While not an exhaustive list, it covers the types you’re most likely to use in practice.
Local variables – Declared inside a function and only used within that scope; best for temporary or isolated values.
Global variables – Declared outside any function and used throughout the project; good for sharing data between functions.
Integer – Whole numbers used for counting, distances, or anything without decimals.
Double – High-precision decimal numbers for mathematical calculations requiring accuracy.
Float – Standard-precision decimal numbers, useful for measurements or calculations.
C-style String – Text values, used for messages, labels, or displaying readable output.
Boolean (bool) –
true
orfalse
values for logic and decision-making.Arrays – Fixed-size collections of elements of the same type.
2D Arrays – Arrays of arrays; ideal for representing rows, grids, or table-like data.
Declaring and Assigning a Variable#
To create a variable in C++, you must declare its type followed by the variable name, then optionally assign a value using the =
operator:
int distance = 100;
When naming a variable, the following rules must be adhered to:
The name cannot contain special characters (e.g., an exclamation point).
The name cannot begin with a number.
The name cannot use spaces.
The name cannot be a reserved word in VEXcode (e.g. Drivetrain).
Variable names are case-sensitive (e.g.,
Distance
anddistance
are different variables).
Local Variables#
Local variables are defined inside a function or block of code. They are only accessible within the scope of that function or block and are not visible outside of it.
void show_local() {
// This variable only exists inside this function
int angle = 90;
Brain.Screen.print("%d", angle);
}
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// Begin project code
show_local();
}
Local variables are commonly used to store temporary values that are only relevant within a specific function or part of the program.
Global Variables#
Global variables are defined outside of any function or block. They can be accessed and read anywhere in the program, including inside functions.
Note: Global variables are accessible from anywhere in the program, which can make them convenient for sharing data across functions. However, relying heavily on global variables can lead to unintended side effects, as changes to the variable in one part of the program may affect other parts unpredictably. For this reason, local variables are generally preferred when possible, as they limit a variable’s scope to the specific function where it is defined. This reduces the likelihood of conflicts and makes debugging easier.
// The variable is defined outside a function
int angle = 90;
void show_global() {
// You can access 'angle' inside a function
Brain.Screen.print("%d", angle);
Brain.Screen.newLine();
}
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// Begin project code
show_global();
Brain.Screen.print("%d", angle);
}
In C++, global variables can be directly accessed and modified from within functions without any special keywords. However, if you have a local variable with the same name, you can use the scope resolution operator ::
to access the global variable.
// Define the global variable
int count = 0;
void increase_count() {
// Global variables can be accessed directly
count = count + 1;
Brain.Screen.print("Count: %d", count);
Brain.Screen.newLine();
}
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// Begin project code
increase_count();
increase_count();
}
Types of Data#
C++ variables can store various types of data, each suited for different use cases. Below are the most commonly used types:
Integer#
An integer (int
) is a whole number.
int distance = 100;
// Move the robot forward for the variable value in mm
Drivetrain.driveFor(forward, distance, mm);
// Add to the variable and move forward the new value,
// for 200mm total
wait(1, seconds);
distance = distance + 100;
Drivetrain.driveFor(forward, distance, mm);
Double#
A double is a decimal-point number with high precision (typically 64-bit).
// Store a value with decimal points
double raw_value = 0.88;
// Print the decimal value as a percentage
Brain.Screen.print("%.1f%%", raw_value * 100);
Float#
A float is a decimal-point number with standard precision (typically 32-bit).
// Store a value with decimal points
float raw_value = 0.88;
// Print the decimal value as a percentage
Brain.Screen.print("%.1f%%", raw_value * 100);
String#
A C-style string (char variable_name[]
) is an array of characters, commonly used for text.
// Set the variable to a string then print the string
char message[] = "Ready!";
Brain.Screen.print(message);
Boolean#
A Boolean (bool
) represents true
or false
values.
// Set the state of the variable
bool delivered = false;
// Print different messages depending on the Boolean.
if (delivered) {
Brain.Screen.print("Package delivered!");
} else {
Brain.Screen.print("Delivering...");
}
Booleans can be changed at any point in the project.
// Print the value of the delivered variable
bool delivered = false;
// Print different messages depending on the Boolean.
if (delivered) {
Brain.Screen.print("Package delivered!");
} else {
Brain.Screen.print("Delivering...");
}
wait(2, seconds);
// Clear the screen and print the value of the variable again
Brain.Screen.clearScreen();
Brain.Screen.setCursor(1,1);
delivered = true;
// Print different messages depending on the Boolean.
if (delivered) {
Brain.Screen.print("Package delivered!");
} else {
Brain.Screen.print("Delivering...");
}
Arrays#
Arrays are fixed-size collections of elements of the same type. It is good practice to know the length of the array and declare at the time of creation.
// Declare an array of 5 integers
int distances[5] = {100, 200, 150, 300, 250};
// Drive and turn 4 times to move the distance of the first four distance of the array
for (int i = 0; i < 4; i++) {
Drivetrain.driveFor(forward, distances[i], mm);
Drivetrain.turnFor(right, 90, degrees);
}
2D Arrays#
A 2D array is commonly used to represent grids, tables, or matrices. Each row represents a specific grouping of data.
// Assign the values in the matrix 2D array
int matrix[3][3] = {
{ 0, 1, 2 },
{ 3, 4, 5 },
{ 6, 7, 8 }
};
// Loop through each row
for (int i = 0; i < 3; i++) {
// Loop through each column in the row
for (int j = 0; j < 3; j++) {
Brain.Screen.print("%d, ", matrix[i][j]);
}
Brain.Screen.newLine();
}
You can modify specific elements within a 2D array:
// Assign the values in the matrix 2D array
int matrix[3][3] = {
{ 0, 1, 2 },
{ 3, 4, 5 },
{ 6, 7, 8 }
};
// Modify the color (in column 2) in row 0
matrix[0][1] = 200;
// Print the modified row from the matrix 2D array
for (int j = 0; j < 3; j++) {
Brain.Screen.print("%d, ", matrix[0][j]);
}