Variables
Variables
• Serves as data storage for the
assigned or calculated value in your
program.
• The value it holds may vary or change
depending on the conditions or
instructions specified in the program
• It requires a data type to identify the
kind of data to be stored and a
variable name.
Variables
• Syntax
dataype variableName;
Data type of a Variable
Data Type Description Arduino
Ram Usage
boolean Holds true or false value 1 byte
char Stores a character value 1 byte
Primary data type for a whole number storage
int 4 bytes
commonly in 16 bits (2 bytes)
float Stores number with floating or decimal point 4 bytes
Rules in naming a
variable
Rules in naming a variable
1. The first character of a variable
name must be a letter either
uppercase and lowercase followed
by a combination of any letters
and numbers. An underscore is the
only acceptable special character.
Rules in naming a variable
2. The use of an underscore to
start a variable is not
recommended since it might
conflict with the computer system
name and cause an error.
Rules in naming a variable
3. A variable name can be of any
length, though the compiler only
checks the first 31 characters of a
variable.
Initializing Variables
Initializing Variables
Initializing a variable means to
assign a starting value to a variable
during declaration or within the
program.
Initializing Variables
Assignment operator will be used to
assign a specific value to be stored in
that variable.
Assignment operator “=“
Constant Variables
Variables that do not and must not
change their value throughout the
program.
Multiple Variables Declaration
Variable declaration can be
separated by a comma(,).
Variable Scope
The scope of a variable refers to the
visibility or accessibility of the
variable within the program.
The part where you declare the
variable may identify the scope of
the variable.
Variable Scope
The scope of a variable refers to the
visibility or accessibility of the
variable within the program.
The part where you declare the
variable may identify the scope of
the variable.
Variable Scope
The scope of a variable refers to the
visibility or accessibility of the
variable within the program.
The part where you declare the
variable may identify the scope of
the variable.
Global Variables
Variables defined at the topmost part
of your program to declare it outside
all function are called global
variables.
These variables can be used and are
accessible to all functions in the
entire program.
Local variables
They are only declared inside the
block of a specific function.