Introduction to Variables
What is a Variable?
In math, it's common to see problems like below:
We can solve for "a" to determine a = 7, right? Then we could solve a variety of additional calculations with that knowledge like:
Variables in programming serve the same role! We use a variable to represent a value, or other information, that then can be used throughout the program.
Why use a Variable?
Consider for a moment: Why would we use a variable when we could simply use the number on its own?
While there are several reasons, one of the biggest is that variables can greatly help with organization in a program and streamline making changes.
Rather than having to hunt down every place a value has been used in a program, if we need to make a change we can adjust just the variable and be done! This helps to prevent mismatches that could come from one value being updated, but another missed.

Variables might also be used to store the results of a calculation in a program to then be used wherever it's needed, such as the values from joystick movements on the gamepad.
Types of Variables
Constant variables do not change after being set. These are often set during initialization or at the start of an OpMode.

These variables might include something like a target velocity, position, or power that will be reused or other settings that need to remain consistent whenever called.
Non-constant variables are often just called variables. These change throughout the program, but may be set during initialization or the beginning of an OpMode to a default.
These variables, for example, may store the results of a calculation run in the OpMode or data collected by a sensor.

Last updated
Was this helpful?

