Last Updated: 11/08/2021

Lesson 10 - Local and Global Variables

Lessons >> Basics >> Lesson 10 - Local and Global Variables

Lesson Contents

Explain the difference between a Global and Local variable
Understand the dangers of Global variables
Show how local variables values can be transferred to functions
Understand how the same variable name can be used multiple times without conflict.
Understand the difference in the declaration of a Global or Local variable

Example 1: LocalvGlobalVariablesv1.ino


Click to Download code:LocalvGlobalVariablesv1.ino

Understanding how Local and Global variables are declared

 
/*  10/08/2021
 *  LocalvGlobalVariablesv1
 *  Understanding the scope of a variable.
 *  Showing the difference between a Global and Local variable
 * 
 */

 //Global variables can be accessed from any function
 //Must be declared outside of any function
 int returnValue;
 int q;

 int multiply(int myValue){
  //Local variables are declared inside a function and only exist within the function
  //When the function completes the Local variable and it's value are destroyed.
  int newValue;
  newValue = myValue * 10;
  return newValue;
 }

void setup() {
  // Serial.begin(9600) starts serial communication. 9600bps bits per second = 1200 characters per second
  Serial.begin(9600);
  //Send script name
  Serial.println("LocalvGlobalVariablesv1...");
  //this will print a blank line
  Serial.println(" ");
}

void loop() {
  //q in this sketch is a Global variable
  for(q=0;q<10;q++){
    //returnValue is a Global variable
    returnValue = multiply(q);
    Serial.print(q);
    Serial.print(" ret Value: ");
    Serial.println(returnValue); 
    delay(500); 
    //This line will not compile because newValue is a Local variable 
    //that is Local to the function multiply
    //Serial.println(newValue);
  }

}

Example 2: LocalvGlobalVariablesv2.ino


Click to Download code:LocalvGlobalVariablesv2.ino

Show how a Global variable can be used in any functions.
Understand the dangers of how Global variables can be corrupted.
Because the value of q is altered in the multiply() function it corrupts the value in the loop() function.

 
/* 10/08/2021
 *  LocalvGlobalVariablesv2
 * 
 * Showing the difference between a global and local variable
 *  Global value q sent to function
 */

 //Global variables can be accessed from any function
 //Must be declared outside of any function
 int returnValue;
 int q;

 int multiply(int myValue){
  //newvalue is Local variable
  int newValue;
  //altering the value of q in the function alters value in loop() and causes the for loop to restart
  q = 10;
  newValue = myValue * q;
  return newValue;
 }

void setup() {
  // Serial.begin(9600) starts serial communication. 9600bps bits per second = 1200 characters per second
  Serial.begin(9600);
  //Send script name
  Serial.println("LocalvGlobalVariablesv2...");
  //this will print a blank line
  Serial.println(" ");
}

void loop() {
  //q is a Global variable
  for(q=0;q<10;q++){
    //returnValue is a Global variable
    returnValue = multiply(q);
    Serial.print(q);
    Serial.print(" ret Value: ");
    Serial.println(returnValue); 
    delay(500); 
  }

}

Example 3: LocalvGlobalVariablesv3.ino


Click to Download code:LocalvGlobalVariablesv3.ino

Transferring the value of a Local variable to a function safely
Ability to ue the same variable name safely.
Understand the destruction of a Local variable when a function has completed
Reducing the number of Global variables.

 
/* 10/08/2021
 *  LocalvGlobalVariablesv3
 * 
 * Showing the difference between a global and local variable
 * q is now Local in multiply function and loop() function so values changed in one do not effect the other.
 * Value of q transferred between functions without the Local version becoming corrupted.
 */

 //There are no Global varoable declared in this sketch
 //Nothing is declared outside of any function

 

 int multiply(int myValue){
  //Local variable
  int newValue;
  //setting the value of q that is now Local does not effect q elsewhere
  int q;
  //because q is destroyed on exiting the function the value of q never rises above 10;
  q = q+10;
  newValue = myValue * q;
  return newValue;
 }

void setup() {
  // Serial.begin(9600) starts serial communication. 9600bps bits per second = 1200 characters per second
  Serial.begin(9600);
  //Send script name
  Serial.println("LocalvGlobalVariablesv3...");
  //this will print a blank line
  Serial.println(" ");
}

void loop() {
  //Local Variable
  int q;
  //Local variable
  int returnValue;
  for(q=0;q<10;q++){
    returnValue = multiply(q);
    Serial.print(q);
    Serial.print(" ret Value: ");
    Serial.println(returnValue); 
    delay(500); 
  }

}

Additional Resource Links

Don't forget to use the Reference in your Arduino IDE and look at Scope

Comments


This site has been designed to be child friendly, this means that comments cannot be added to videos or directly to the site.
To add a comment or ask a question please email the address in this image: and use Lesson 10 - Local and Global Variables as a reference.