Programming‎ > ‎RobotC‎ > ‎1. The Basics‎ > ‎

b. Variables

Variables are equivalent to Proper Names in English.  This is a way that we name things for later use.  Another way to think of a variable is a placeholder or container for something.  In algebra, variables like x and y are commonly used to represent a number of value.

A variable is made up of three parts:
  • Name: Just like a person's name, this uniquely identifies the variable.  You cannot create two variables with the same name in the same area.  Areas in code are often referred to as scope.
  • Value: The information that is being represented by the variable.  If a variable is a container then the value is the information inside of the container.
  • Data Type: The data type describes the category of information that is stored.  Information that is stored can be a limitless number of data types.  RobotC has some pre-defined data types and programmers can create their own data types.
A programmer can do three things with a variable:
  • Declare it (Declaration): In order to use a variable you must first declare or define it.  This is required and must be done before values can be assigned or read from it.  The syntax for declaring a variable is:
    Data Type Variable Name = Initial Value;
    • Example:
      int speed = 50;
      
      • int - The data type that says the variable will be a whole number like 1, 2, 3, 4 ...
      • speed - The name of the variable, in this case used to store the speed of the robot.
      • 50 - The initial value of the variable.
  • Assign a value to it (Assignment): Values of a variable can be changed.  The act of assignment is to assign a new value to a variable.
    • Example:
      speed = 75;
      
      • In this case the variable speed has been changed from 50 to 75.
  • Read the value from it (Use)
    • Example:
      motor[motorC] = speed;
      
      • In this case the motor labelled motorC will be set to the value of the variable speed which is currently set at 75.
  • Compare the value of the variable:

Previous Page: Syntax  Next Page: Data Types
Comments