Type Here to Get Search Results !

How To Use Variable Scope In Arduino IDE

So far we saw how variables are declared, now let’s discuss on the location of a variable declaration. The location of a variable declaration in a sketch actually determines where that variable can be used, or what is known also as the "variable scope". We have two types of variable scope: "global" variables and "local" variables. Global variables are declared outside a specific, function at the beginning of the source code. It can be used by all functions in the sketch regardless of location and this is very useful if we would like to use the variable values across different functions.



We will use them allot to define pin numbers, set up thresholds, or define constants, so that all functions in our sketch can use the same values. Now if the variable are declared inside a specific function, it would become a local variable and would be unavailable outside that specific function.

We will see later that some local variables can also be used inside "for loops", as counters. Where only that statement can use it and not the rest of the function, we will see that later. So, why not to declare all variables as global ? Instead of using local variables well, using too much global variable makes it more harder to read and understand the software. Since any line of code anywhere in the program can change the value of the variable at any time

So in that case to understand the usage of that global variable, we will need to analyze the all program. So we need to balance between the usage of global variables and local variables. Please watch this video below to understand this better.



Post a Comment

0 Comments