Fixing "a function-definition is not allowed here before '{' token"
Beginner
Or see every lesson.
- 455parts on the bench
- 43boards running now
- 1.00xreal time, on every board
This error means a function starts inside another function, which C++ does not allow. When the compiler meets void loop() { while it still thinks it is inside setup(), it stops there. The cause is nearly always a missing } somewhere above that line: the end of the function before it, or the end of an if, for or else inside it. Find the opening brace that has no partner, add its }, and both this error and the "expected '}'" that comes with it go.
Mokxi compiles with clang, which words it function definition is not allowed here. The circuit above is a potentiometer on A0 of an Uno, printing "A0 = 512 (2502 mV)" five times a second with the knob in the middle. Open it, delete the } at the end of setup() and press Run: the error lands on the void loop() line, and the build tab says "A function starts inside another one. Usually a } is missing at the end of the function above."
The error is on the next function, the mistake is above it
Here is a short sketch with the closing brace of setup() missing. Clang gave two errors: function definition is not allowed here at line 7, column 13, the { of void loop(), and expected '}' at the end of the file, with a note, to match this '{', at line 3, where setup() opens. GCC gives the same pair: a function-definition is not allowed here before '{' token and expected '}' at end of input.
Read the note, not the first error. The note points at the brace that was never closed, and the missing } belongs at the end of that block. Mokxi’s build tab says "The { on line 3 is never closed". Put the } after the last line of setup(), where it belongs. A brace added at the very end of the file instead would leave loop() inside setup(), and the first error would stay.
const int SENSOR_PIN = A0;
void setup() {
Serial.begin(9600);
Serial.println("reading A0");
void loop() {
int raw = analogRead(SENSOR_PIN);
Serial.println(raw);
delay(200);
}A missing brace inside the function above
The brace that is missing is often not the function’s own. In a loop() whose else { digitalWrite(13, LOW); was never closed, the } meant for loop() closed the else instead, so loop() itself stayed open. The error landed on line 15, at the next function, void blinkTwice(), and the note pointed at line 6, the { of loop(), though the brace that was really missing belonged to the else on line 10.
So once the note tells you which function is open, look inside it for the inner block that was never closed. Indented code makes it easy to see: every } should sit straight under the line its block starts on. Auto Format does the indenting for you, with Ctrl+T (Cmd+T on a Mac) in the Arduino IDE and in Mokxi’s code editor, and a missing brace shows up as a function whose last lines are pushed one step to the right.
A brace inside a comment
A } in a comment is not code, so it closes nothing. That catches people who comment out a few lines to try something and take the closing brace with them. In a sketch whose if (digitalRead(2) == HIGH) { in setup() ended with // }, the build stopped with the same pair: function definition is not allowed here at line 8, the { of void loop(), and expected '}', with the note at line 1, where setup() opens. The note names setup() and not the if, because the } meant for setup() closed the if instead.
So when the error appears just after you commented something out, look at the lines you commented first. Take the // off the brace, or comment out the if line along with it, and the braces pair up again.
A function written inside loop() on purpose
Some languages let you write a function inside another one. C++ does not. A void flash() { ... } written inside loop() stopped the build with function definition is not allowed here at its line, and then use of undeclared identifier 'flash' where loop() called it, because the function was never made.
Write the function outside, below loop() or above setup(), and call it from inside. The Arduino IDE and Mokxi both write the declaration that lets loop() call a function written further down, so the order does not matter.
If a small helper really belongs next to its one use, C++ has a way to write it there: a lambda. auto flash = []() { digitalWrite(13, HIGH); delay(100); digitalWrite(13, LOW); }; inside loop(), then flash();, built without a complaint. A lambda is a variable that holds a function, so it ends with a semicolon, and it can only be called inside the function that makes it.
void setup() {
pinMode(13, OUTPUT);
}
void loop() {
flash();
delay(900);
}
void flash() {
digitalWrite(13, HIGH);
delay(100);
digitalWrite(13, LOW);
}Questions
What does "a function-definition is not allowed here before '{' token" mean?
A function starts inside another one. Almost always a } is missing above it: at the end of the function before, or at the end of an if, for or else inside it. Add the missing brace.
Why is the error on void loop() when loop() is fine?
The compiler only notices the missing brace when the next function starts. The mistake is above that line, in setup() or in whatever function comes first.
Can I put a function inside loop()?
No. Write it outside every function and call it from loop(). The Arduino IDE and Mokxi let you call a function that is written further down the sketch.
Is "function definition is not allowed here" the same error?
Yes. It is clang’s wording; GCC, which the Arduino IDE uses, says "a function-definition is not allowed here before '{' token". Mokxi compiles with clang, so its build tab shows clang’s words.
Is this an Arduino error or a C++ error?
C++. Every C++ compiler refuses a function written inside another one, so the fix is the same in any C++ program. GCC words it a function-definition is not allowed here before '{' token, and clang function definition is not allowed here.
Is there any way to write a function inside loop()?
One form is allowed inside a function: a lambda. `auto twice = [](int x) { return x * 2; };` inside setup() built, and `twice(21)` printed 42. For anything longer, write an ordinary function outside every function and call it.
Keep going
Where the facts on this page come from
Build this for real
Open the editor, change a value and watch the number move with it. Nothing to install, and no account needed.