Sub topic 10
Scope and Lifetime: Local, Global, and Static Variables
Assume you can open, verify, and upload an Arduino sketch, and that you know basic C++ syntax, pins, and setup() and loop(). This chapter focuses on where a variable can be used and how long its value remains available.
Scope and lifetime
A variable's scope is the part of the program where its name can be used. Its lifetime is the period during which the variable exists in memory.
| Variable kind | Declared in | Usable from | Lifetime |
|---|---|---|---|
| Local | A function or block | That function or block | Usually one call or block entry |
| Global | Outside all functions | All functions in the file | From startup until reset or power loss |
| Static local | A function or block with `static` | That function or block | From startup until reset, but keeps its value |
A variable declared inside setup() cannot be used in loop(). A variable declared outside every function can be used by both. However, global variables are shared state, so many functions can change them. That makes programs harder to understand when there are many globals.
const byte ledPin = LED_BUILTIN;
unsigned long blinkCount = 0; // Global: shared by setup() and loop()
void recordBlink() {
static unsigned int calls = 0; // Local scope, persistent lifetime
calls++;
blinkCount++;
Serial.print("Function calls: ");
Serial.print(calls);
Serial.print(", total blinks: ");
Serial.println(blinkCount);
}
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
digitalWrite(ledPin, HIGH);
delay(250);
digitalWrite(ledPin, LOW);
delay(250);
recordBlink();
}The name calls is visible only inside recordBlink(). Its value is created once and preserved between calls because it is static. Without static, calls would be created again with value zero each time, so the output would always report one call.
The global variable blinkCount is visible in both setup() and loop(), and also inside recordBlink(). It is initialized before setup() starts. On a reset or power cycle, it starts again at zero.
Blocks create smaller scopes
Braces create a block. A variable declared inside a block is unavailable outside it:
void loop() {
for (byte i = 0; i < 4; i++) {
int reading = analogRead(A0);
Serial.println(reading);
}
// i and reading cannot be used here.
}The loop variable i exists only inside the for statement's block. The reading variable also disappears when that block ends. This limits accidental changes and can reduce memory use.
Common errors and design choices
A local variable is usually the best default. Use it when only one function needs the value. Use a global when several parts of the sketch must share a value, such as a pin number, a sensor reading, or a timing timestamp. Prefer const for shared values that must not change.
Use a static local when a function needs private memory between calls. This is useful for a counter or a previous sensor state without exposing that state to the rest of the sketch.
Avoid declaring a local variable with the same name as a global variable. The local variable then hides the global one inside its scope, which can produce confusing results.
Practice
When debugging scope, ask two questions:
- Which braces contain the declaration?
- Should the value reset on every function call, or persist between calls?
Choose the narrowest scope that meets the requirement. This makes the sketch easier to test and protects data from unrelated code.
Exercise
In the example, what happens if static is removed from calls, and why?
Show answer
calls is recreated and initialized to zero each time recordBlink() runs. It is then incremented to one, so every report shows one function call.