Strange compiling problem

Got a funny error while compiling a sketch today.

Original code, which worked fine:

setup() {
 ...
 uint32_t c = randomColor(10, getMaxLuminocity());
 ...
}

uint32_t randomColor(int iMin, int iMax) {
 ...
}

setup() is the first function in the sketch. And there are a few other functions between setup and randomColor, like the getMaxLuminocity function.

Changed this to:

setup() {
 ...
 uint32_t c = randomColor(10, getMaxLuminocity());
 ...
}

uint32_t randomColor(const int iMin, const int iMax, uint8_t mode = 0) {
 ...
}

Then I got an error while compiling saying that randomColor was not defined. I even tried to add a third paramter 0 to the call to randomColor in setup(), but still got the error.

Had to move the randomColor function above the setup function in the sketch to fix this error.

Bug in compiler?

The Arduino IDE allows functions to appear in the code before they are declared. This is non standard C++ behavior. To achieve this, it automatically builds function prototypes for all functions it finds (in the .ino). It sometimes makes a mess of this by getting them out of order etc. It looks like it does not handle default parameters nicely either.

If you want to use a default parameter, you have to manually declare the prototype (including the default parameter, so

uint32_t randomColor(const int iMin, const int iMax, uint8_t mode = 0) ;

setup() {
 ...
 uint32_t c = randomColor(10, getMaxLuminocity());
 ...
}

uint32_t randomColor(const int iMin, const int iMax, uint8_t mode) {
 ...
}

should work as expected.

That explain why. Thanks :)