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 s= 10;
int f;
void setup()
{
Serial.begin(9600);
}
void loop(){
int x= 15;
int y=16;
Serial.println("Result for multiplication is:");
f=x*y*s;
Serial.println(f);
delay(3000);
}
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.
{
Serial.begin(9600);
}
void loop(){
int i= 15;
int j=16;
int k;
Serial.println("Result for Addition is:");
k = AdditionFunction(i,j);
Serial.println(k);
delay(3000);
}
int AdditionFunction(int x, int y)
{
int result;
result = x+y;
return result;
}
Constants
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 e;
int d = 10;
int c = 15;
const int a=78;
void setup()
{
Serial.begin(9600);
Serial.print("result for multiplication is:");
b = a*2;
Serial.println(b);
Serial.print("Result for addition is:");
e = b+d;
Serial.println(c);
}
void loop(){
}
Conclusion
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.