You’re coding away happily, excited to upload your Arduino sketch, when BOOM — red text explodes in the bottom window. The error: “Variable or field declared void”. What does that even mean? Don’t worry! You’re not alone, and you’re definitely not doomed. Let’s break it down and get that code back on track.
TL;DR:
This error is usually because of a function definition or declaration that has incorrect syntax. It can happen if a function has wrong parameter types, missing names, or misplaced brackets. Double-check your function headers and parameters. Once you spot the sneaky syntax issue, your code should compile smoothly!
What Does This Error Mean?
The message “Variable or field declared void” sounds cryptic. But it’s essentially the compiler saying, “Hey! Something’s wrong with this function.”
In C++ (which the Arduino language is based on), every function needs to follow a strict format. When the compiler sees something off, it might blurt out this weird message. Let’s decode it!
Common Causes (and How to Fix Them)
Here are the most likely reasons and ways you can fix them. Look out for these!
1. You Forgot to Name a Function Parameter
Bad:
void blink(int);
Oops! That looks like a declaration without defining what the function does — completely fine. But check this out:
void blink(int) {
digitalWrite(LED_BUILTIN, HIGH);
}
This is where the problem hits. The compiler expects a parameter name because it’s the actual function definition.
Fix:
void blink(int times) {
digitalWrite(LED_BUILTIN, HIGH);
}
Easy fix! Just name the parameter.
2. Wrong Type or Syntax in the Function Header
Let’s say you’re trying to pass a function to another function, like callbacks do. You might have written:
void doSomething(void callback()) {
callback();
}
Ideally okay — but depending on context, this might crack. The Arduino IDE sometimes goes bonkers with this kind of syntax.
Fix: Be explicit with your parameters.
void doSomething(void (*callback)()) {
callback();
}
See that (*callback)() part? That’s how you correctly define a function pointer. Super geeky, but super important.
3. You Declared a Function inside Another Function
This is a no-no in Arduino (and C++ in general). Look at this:
void loop() {
void myInnerFunction() {
digitalWrite(LED_BUILTIN, HIGH);
}
}
Whoa! One function inside another? The compiler has no idea what to do with this. Hence, the cryptic error.
Fix: Always declare your functions outside other functions.
void myInnerFunction() {
digitalWrite(LED_BUILTIN, HIGH);
}
void loop() {
myInnerFunction();
}
Let’s Talk Types
Sometimes, parameter types can be mixed up. Make sure you use valid types, like:
intfloatcharString(with capital S!)
And don’t forget custom types like byte or boolean are okay too. But you need to be consistent.
Bad:
void setLED(state) {
digitalWrite(LED_BUILTIN, state);
}
What’s missing? The type of state. The compiler’s confused — is it a number? A boolean? A cookie?
Fix:
void setLED(boolean state) {
digitalWrite(LED_BUILTIN, state);
}
There we go! Crystal clear.
Pit Stop: Look at This Classic Mistake
Suppose you’re in a rush and write:
void turnOnLED(int pin, );
Oops again! An extra comma with no second parameter = angry compiler.
Fix:
void turnOnLED(int pin);
Now it’s happy.
Handy Checklist Before You Hit Upload
Use this list whenever you get the dreaded error:
- Did I forget to name a parameter inside a function?
- Am I using proper types for all parameters?
- Are my function declarations and definitions matching?
- Did I accidentally put a function inside another one?
- Am I using correct function pointer syntax?
- Any unnecessary characters? (like commas, semicolons)
Every one of those can trip you up.
Bonus Tip: Use Comments!
Sometimes you can get confused between declarations and definitions. Try this:
// Declaration
void flashLED(int times);
// Definition
void flashLED(int times) {
for (int i = 0; i < times; i++) {
digitalWrite(LED_BUILTIN, HIGH);
delay(200);
digitalWrite(LED_BUILTIN, LOW);
delay(200);
}
}
Organizing your code cleanly helps the compiler — and your brain.
Real Life Example
You wrote an awesome program:
void loop() {
blinkLED(3);
}
void blinkLED(int) {
for (int i = 0; i < 3; i++) {
digitalWrite(LED_BUILTIN, HIGH);
delay(500);
digitalWrite(LED_BUILTIN, LOW);
delay(500);
}
}
But oops! It won’t compile. The culprit?
Function’s missing parameter name!
Fix:
void blinkLED(int times) {
for (int i = 0; i < times; i++) {
digitalWrite(LED_BUILTIN, HIGH);
delay(500);
digitalWrite(LED_BUILTIN, LOW);
delay(500);
}
}
And BOOM 💥 — it uploads!
Wrapping It Up
That “Variable or field declared void” error may sound scary, but it’s actually just your compiler’s awkward way of asking: “Are your functions written correctly?”
Once you know the usual suspects — forgotten names, messy syntax, wrong function location — you’ll track ’em down like a bug-detective 🕵️♀️.
Now go, upload great things. Your Arduino is waiting!

