Beginner Series Lesson 5: Variables
In this lesson we will learn the fundamentals of variables and variable math

Copy of IMG_9828

Variables are a fundamental part of all programs and programming languages.  Just like in math, variables in coding are placeholders for numbers and can be used in all the same ways.  All programmers should understand how variables work and how to use them.

What Are Variables?

Variables in programming are used for data storage.  You can save a value by naming it as a variable, then you can recall the value by calling the variable.

Storing the variable

Now the value 115200 is stored in the variable x .  Every time we use x , we can imagine that it is replaced with the number 115200

is the same as

In this example, using a variable would just be adding an extra step.  In many cases, proper variable usage could save you a lot of work.



Using Variables

There are three steps to using a variable:

  1. Initialization
  2. Storage
  3. Call

Variable Initialization

Before you use a variable, you have to initialize it.  Initialization always follows the format

For example

would initialize an integer variable named Example.

Data Types

Choosing the right data type is important because it determines how much data each variable can store.  Some common ones are listed below

  • byte  – A byte. Can be any whole number from 0 to 255
  • int  – An integer. Can be any whole number from -32,768 to 32,767
  • long – A long. Can be any whole number from -2,147,483,648 to 2,147,483,647

You might recognize byte  from your IR sensor codes byte bt1 . bt1  is your variable name, and it’s data type is a byte .

Variable Names

A variable can have any name you want, as long as there are no spaces or special characters.


Note that the starting value of a variable is always zero (HappyFace123 = 0).


Declaring with Value

When you declare a variable, you can also set it to an initial value


HappyFace123  now starts out equal to 10.


Variable Scope

The scope of a variable is an important thing to keep in mind when programming.  The scope determines where in your program your variable exists.  If you try to use a variable outside of it’s scope, the compiler will show an error saying “not declared in this scope”.

A general rule is “A variable can only be used within the brackets it is initialized in”

Here’s a few examples:

Ex. 1

Scope Brackets

x  is initialized within the brackets of void setup(){ , so it can only be used within these brackets. x = 2  works fine, since it is within void setup(){  as well.  When we try to set  x = 1  in void loop() , we get a scope error because it is not within the same set of brackets.

Ex. 2

Scope Brackets2

x  is initialized within the brackets of if(true){ , so it can only be used within these brackets. x = 1  works fine, since it is within if(true){ .When we try to set  x = 2  in void setup(){  outside of the if-statement, we get a scope error because it is not within the same set of brackets.


scope anywhere

In this example, x  is initialized before void setup(){   and outside of any brackets.  This is called a global variable and can be used anywhere.


Variable Math

The best thing about variables is how easily they can be manipulated.  In programming, variables can be changed using normal mathematical operations.

Mathematical Operations

x = y + z   Addition

x = y - z   Subtraction

x = y * z    Multiplication

x = y / z   Division

Ex. 1

The variable x is now equal to 7.

Ex. 2

A special thing that you can do with programming variables is a summation.  This allows you to use a variable in it’s own equation.

The old value of x  was 3, so it was x = 4 + 3 . The new value of x  is now 7.


Activity: Now You’re Thinking With Variables

Using the setup code provided, get your CoDrone to take off then fly in a square pattern.  You are only allowed to use the existing variables.  You can’t use any numbers!



In the next lesson, we’ll utilize variables and variable math to create and use Flag variables.