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

// 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);
}

}

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

int main() {

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

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

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

#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;
}

:slight_smile:

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