Variable scope and constants in Arduino

When writing a program for a specific task there are some values that remain constant throughout the program but some values keep on changing. Similarly, the declaration of each value either constant or variable is done with respect to its usage. This article briefly explains the variable scope and constant in the Arduino programming language.

Variable Scope in Arduino

Scope is a property of variables used in programming languages. The scope can be defined as the area of the program where the variables are defined. Furthermore, the variables are classified on the basis of the region in which they are declared. Based on the scope of the variables they can be divided that into three categories are:

  • Local Variables.
  • Global Variables.
  • Variables used in formal parameters

Local Variables

The variables that are declared inside the setup function or in the loop function are called the local variables. These variables are called local variables as they can only be accessed from within the loop or setup functions, they are not accessible outside these functions. In other words, it can be said that the scope of these variables is limited.

Global Variables

When the variables are declared outside the setup and the loop functions, such variables are called global variables. These variables can be accessed from anywhere either from inside or outside of the set-up and loop function. There is a common practice that the global variables are mostly declared at the start of the program above the setup function.

To give a better understanding of the classification of the local and global variables. A simple Arduino program is compiled. The program is performing multiplication operations of local and global variables in a loop and the loop is infinitely running with a delay of three seconds.

int t= 10;
int s= 10;
int f;
void setup()
void loop(){
int x= 15;
int y=16;
 Serial.println("Result for multiplication is:");

Formal Parameters Variables

The variables which are used when a function is to be defined are called the formal parameter variables. These parameters do not need declaration as they are used outside the setup or loop function. The defined function is called in the loop block or in the setup block by using the name of the function and then the formal parameter variables are replaced by the local variables.

The formal parameter variables do not have any values and only their data type is specified in the defined function. The data type of the formal parameter variables and the data type of the local variables should be the same. To further illustrate the concept of formal parameter variables an example of a simple Arduino code is given. The code performs a simple addition task by calling an additional function the loop is running with a delay of 3 seconds.

void setup()
void loop(){
int i= 15;
int j=16;
int k;
 Serial.println("Result for Addition is:");
k = AdditionFunction(i,j);
int AdditionFunction(int x, int y)
  int result;
  result = x+y;
  return result;


Like variables, constants are also defined in Arduino programs. If a program is using some mathematical equations having universal constants like Boltzmann constant, pi radians, charge on an electron are to be defined as constants then it can be done by using the keyword const. However, not only universal constants are declared as constant, but it could be any other value. To give a clearer picture of the constants an Arduino code has been compiled doing a mathematical operation that is multiplication is performed using both constants and variables.

int b;
int e;
int d = 10;
int c = 15;
const int a=78;
void setup()
 Serial.print("result for multiplication is:");
 b = a*2;
 Serial.print("Result for addition is:");
 e = b+d;
void loop(){


In a nutshell variables and constants are a very important part of Arduino programming especially for performing mathematical and logical operations that are required for a specific task. As a result, these variables and constants control the flow of the program indirectly. This write-up is a brief explanation of the scope of variables and constants. The types of scope of variables are also discussed. Furthermore, to illustrate the scope in detail the example code for variables and constants are also given.

About the author

Aaliyan Javaid

I am an electrical engineer and a technical blogger. My keen interest in embedded systems has led me to write and share my knowledge about them.