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

**URL:** <https://forum.arduino.cc/t/error-a-function-definition-is-not-allowed-here-before-token/572040>\
**Category:** Programming\
**Created:** [February 5, 2019, 12:06am UTC](https://forum.arduino.cc/t/error-a-function-definition-is-not-allowed-here-before-token/572040 "2019-02-05T00:06:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nhylan1](https://avatars.discourse-cdn.com/v4/letter/n/9de053/32.png) [@nhylan1](https://forum.arduino.cc/u/nhylan1)\
**Post date:** [February 5, 2019, 12:06am UTC](https://forum.arduino.cc/t/error-a-function-definition-is-not-allowed-here-before-token/572040/1 "2019-02-05T00:06:28Z")

</div>

// Example program  
#include   
#include   
using namespace std;

int main() {

int test(int x = 4, int y = 6)  
{  
if (x \<= y)  
return y - x;  
else  
return test (x - 1, y - 1);  
}

}

---

<div class="post-metadata">

**Author:** ![PieterP](https://avatars.discourse-cdn.com/v4/letter/p/ecccb3/32.png) [@PieterP](https://forum.arduino.cc/u/PieterP)\
**Post date:** [February 5, 2019, 12:16am UTC](https://forum.arduino.cc/t/error-a-function-definition-is-not-allowed-here-before-token/572040/2 "2019-02-05T00:16:58Z")

</div>

My troll detector is tingling, but here we go ...

Please use code tags when posting code, and format it correctly.  
This is an Arduino forum. You did not post Arduino code.  
You didn't even have the courtesy to ask an actual question.  
The compiler is right: a function definition is not allowed there. Why are you defining the test function inside of the main function?  
Do you really want to recursively call test until y overflows?

Pieter

---

<div class="post-metadata">

**Author:** ![johnwasser](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnwasser/32/214270_2.png) [@johnwasser](https://forum.arduino.cc/u/johnwasser)\
**Post date:** [February 5, 2019, 1:41pm UTC](https://forum.arduino.cc/t/error-a-function-definition-is-not-allowed-here-before-token/572040/3 "2019-02-05T13:41:11Z")

</div>

```cpp
int main() {

int test(int x = 4, int y = 6) 
  {

```

In C and C++ you can't declare a function inside a function.

---

<div class="post-metadata">

**Author:** ![PieterP](https://avatars.discourse-cdn.com/v4/letter/p/ecccb3/32.png) [@PieterP](https://forum.arduino.cc/u/PieterP)\
**Post date:** [February 5, 2019, 6:20pm UTC](https://forum.arduino.cc/t/error-a-function-definition-is-not-allowed-here-before-token/572040/4 "2019-02-05T18:20:44Z")

</div>

> johnwasser:  
> In C and C++ you can't declare a function inside a function.

```arduino
#include <iostream>
#include <functional>

using namespace std;

int main() {
  using int_t = unsigned int;
  function<int_t(int_t, int_t)> test = [&test](int_t x, int_t y) {
    if (x <= y)
      return y - x;
    else 
      return test (x - 1, y - 1); 
  };
  cout << test(6, 4) << endl;
}

```

🙂

(Note that signed integer overflow is undefined behavior in C++, so if you use `int` for your data types, the program crashes.)

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 5, 2021, 11:37pm UTC](https://forum.arduino.cc/t/error-a-function-definition-is-not-allowed-here-before-token/572040/5 "2021-05-05T23:37:10Z")

</div>


