the “function not declared in this scope” error in Arduino IDE is one of the most common errors new Arduino developers hit, and understanding why it happens makes it easy to fix.
What the error means
In C/C++ (which Arduino uses), functions need to be declared before they’re called. If you call a function on line 20 but define it on line 40, the compiler doesn’t know what the function is at the point you call it. Arduino IDE’s preprocessor handles some of this automatically, but there are cases where it fails.
The error message:
'functionName' was not declared in this scope
means the compiler hit the function call and had no prior knowledge of that function’s existence.
Cause 1: Function defined after it’s called (most common)
void loop() {
blinkLED(); // called here, but defined below
}
void blinkLED() { // defined here
digitalWrite(13, HIGH);
delay(500);
}
Fix: Add a function prototype before setup():
void blinkLED(); // prototype at the top
void setup() { }
void loop() {
blinkLED();
}
void blinkLED() {
digitalWrite(13, HIGH);
delay(500);
}
Or simply move the function definition above the place it’s called.
Cause 2: Multiple .ino files in the sketch
If your sketch has multiple .ino files, Arduino IDE concatenates them alphabetically. Functions in a file that comes alphabetically after the file calling them may not be declared yet.
Fix: Add prototypes for all cross-file functions in the main .ino file, or rename files to control concatenation order.
Cause 3: Function inside a class or namespace
If a function is defined inside a class or namespace, calling it without the proper scope operator will produce this error.
Fix: Use the correct scope – ClassName::functionName() or bring it into scope with using.
Cause 4: Typo in the function name
The error also appears if the function name in the call doesn’t exactly match the definition – including case sensitivity. blinkled() and blinkLED() are different functions in C++.
Cause 5: Missing library or header
If the function comes from a library that hasn’t been included, the compiler doesn’t know it exists. Add the appropriate #include at the top of your sketch.
Quick diagnostic
Look at the exact function name in the error message, search your code for that name, and confirm: is it defined? Is it defined before or after the call? Is the spelling identical?