What is Variable Scope?

Variable Scope

Variable scope relates to visibility, the larger the scope, the more portions of a program that will have visibility of that variable. In general you want to limit scope as much as possible, giving visibility to as few portions of the program as necessary.

Global Variables

Let’s imagine we have a calendar online and anyone in the world can read and write to this calendar. In the world, the greatest visibility would be to give access to anyone on the globe and this relates to a global variable. A global variable in software is one that is directly accessible by the entire program. An example in Arduino would be when you define a variable at the top of a program, variables defined outside of the curly braces of a function are considered global and that means that every function will be able to read and write to this variable.

Member Variables

Now let’s imagine another calendar but this time it’s not online, it’s a physical calendar in your home. This calendar in your home has limited access, the world cannot see this calendar an the only way to read or write to this calendar is to enter the confines of your home. We can relate the walls of your home to the curly braces of a function, only that portion of the program contained within the curly braces of a function can read or write to variables that are created within the same curly braces.

Formal Parameters

Finally, let’s imagine you get a call from your doctor telling you that you’ve been scheduled for a 6 month follow up on January 1st. Now you have a situation where you are inside your home getting outside information that affects your calendar. The doctor still can’t directly access your home calendar but they can provide you with the information and you can decide whether or not to adjust your calendar. This relates to a variable that is passed into a function by way of a parameter, other portions of the program that call this function can provide information to the function but ultimately still cannot access the member variables.

Caveat

There are always caveats right? In Arduino/C++ we have direct access to memory so it is possible that these lines can be blurred. Basically it comes down to formal parameters and whether a parameter is passed by reference or by value. A parameter passed by value can be thought of as a copy, this is a situation where the function only has read access but a pass by reference variable is providing a pointer to the memory location so this would allow the code within the function to have direct access to read and/or write to a variable.

Luckily for those of you starting out, in general this is hidden from you unless you are using pointers but if you know how to use pointers you are likely not just starting out but pointers is a whole other topic for another day.

Example

// This is a global variable, everyone has access
int globalVariable = 10;


void customFunction(int parameterVariable) {
  Serial.println("\n***customFunction() START");

  // I can read the global variable too
  Serial.print("The global variable was changed to 20 by setup(): ");
  Serial.println(globalVariable);

  // Show that the value is 10, as it was in the other function
  Serial.print("I've been passed a parameter: ");
  Serial.println(parameterVariable);

  // Muahaha, I'm going to change this value
  parameterVariable = 0;
  Serial.print("I've changed the parameter value to 0: ");
  Serial.println(parameterVariable);

  Serial.println("***customFunction() END\n");
}

void setup() {
  Serial.begin(9600);
  
  // This is a member variable, only I have access
  int memberVariable = 10;

  Serial.println("\n***setup() START");

  // We have read access to the global variable
  Serial.print("The global variable should be 10: ");
  Serial.println(globalVariable);

  // We have write access to the global variable
  globalVariable = 20;
  Serial.print("I changed the global variable to 20: ");
  Serial.println(globalVariable);

  // We will call the custom function and provide access to our variable
  customFunction(memberVariable);

  // We passed by value so customFunction can do all it wants, we really just gave it a copy.
  Serial.print("The variable was passed by value so it should still be 10: ");
  Serial.println(memberVariable);

  Serial.println("***setup() END\n");
}

void loop() {

}

 

Leave a Reply

Your email address will not be published. Required fields are marked *