'Function Not Declared in This Scope' Arduino Error: Why It Happens and How to Fix It

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?

The function prototype approach is the cleanest fix for this. Adding void functionName(); at the top of the file tells the compiler the function exists and what it returns before the actual definition appears. It’s standard C++ practice and worth learning early rather than just reordering code every time.

the multi-.ino file concatenation behavior is something that trips people up badly. arduino IDE stitches files together alphabetically which is not intuitive at all. if you’re splitting a sketch across multiple files, prototypes in the main file are basically mandatory.

Case sensitivity in C++ catching people is a classic. blinkLED and blinkled are completely different identifiers. The error message gives you the exact name it can’t find, so comparing that character by character against your definition usually finds typos quickly.

This error is a good introduction to the concept of compilation order and the difference between declaration and definition in C/C++. Understanding why the compiler needs to see a declaration before a call – because it needs to know the return type and parameters to generate correct machine code – makes the fix intuitive rather than just a workaround to memorize.

Missing library include is worth checking if the function name looks like it should be a known library function. A lot of new Arduino users install a library, use its functions, and forget to add the #include. The error message alone doesn’t distinguish between “function doesn’t exist” and “function exists in a library you haven’t included.”.