Arduino build error

error: a function-definition is not allowed here before '{' token

A function can only be defined at the top level of the file. This one sits inside the curly brackets of another function.

  • Build error
  • 2 common causes
  • Seen 2,761 times in failed builds on Mokxi in the 30 days to November 16, 2026.
The message

What the Arduino IDE prints

Output window
sketch.ino: In function 'void setup()':
sketch.ino:5:22: error: a function-definition is not allowed here before '{' token
    5 |   void blink(int pin) {
      |                       ^
exit status 1

It nearly always means a closing brace is missing above it, so the compiler thinks the earlier function is still open.

Mokxi's compiler, clang, words it as function definition is not allowed here.

Causes

The 2 usual causes with the fix

1A closing brace is missing before the new function

The function above never closed, so the next one starts inside it. Add the brace.

Broken
void setup() {
  Serial.begin(9600);
  Serial.println("start");

void loop() {
}
Fixed
void setup() {
  Serial.begin(9600);
  Serial.println("start");
}

void loop() {
}

Mokxi’s build tab says: A function starts inside another one.

Try this fix in Mokxi

2A helper function was written inside setup()

C++ has no functions inside functions. Move the helper out, above or below setup().

Broken
void setup() {
  Serial.begin(9600);
  int twice(int n) {
    return n * 2;
  }
  Serial.println(twice(4));
}

void loop() {
}
Fixed
int twice(int n) {
  return n * 2;
}

void setup() {
  Serial.begin(9600);
  Serial.println(twice(4));
}

void loop() {
}

Mokxi’s build tab says: A function starts inside another one.

Try this fix in Mokxi

FAQ

Questions and answers

What does this error mean?

A function definition, with its own curly brackets, was found inside another function. C++ does not allow that.

Why does it point at a function that looks fine?

The real fault is a missing brace above it. The function it names is only where the compiler noticed.

Where should my own functions go?

At the top level of the sketch, outside setup() and loop(). In a .ino file their order does not matter.