Functions below void loop() cause compile errors

I upgraded my IDE, and now I am having a persistent (and very frustrating) problem where if I define a function below void loop(), I get the error:

'function' was not declared in this scope 

Here is some code that used to compile perfectly back in an older version of the IDE but now it's causing the compile problem in Arduino IDE 1.6.9:

//  TwinkleSparkle2014 is a program that lets you make an LED strip //  "twinkle" all the time and "sparkle" at a defined interval // //  Daniel Wilson, 2014 // //  With BIG thanks to the FastLED community and especially Mark //  Kriegsman whose "Fire2012" simulation inspired this program. //  Some of Mark's original code and comments persist in this work. /////////////////////////////////////////////////////////////////////////////////////////// #include "FastLED.h" #define NUM_LEDS           120 #define DATA_PIN           3 #define CHIPSET            NEOPIXEL  // make sure you enter the correct chipset #define COLOR_ORDER        GRB       // and RBG order #define BRIGHTNESS         250 #define FRAMES_PER_SECOND  30 #define COOLING            20         // controls how quickly LEDs dim #define TWINKLING          100       // controls how many new LEDs twinkle #define FLICKER            50        // controls how "flickery" each individual LED is CRGB leds[NUM_LEDS]; static int beatInterval =  8912;     // the interval at which you want the strip to "sparkle" long nextBeat =            0; long nextTwinkle =         3000;     // twinkling doesn't start until after the sanity check delay unsigned int seeds =       0; long loops =               0; long deltaTimeTwinkle =    0; long deltaTimeSparkle =    0; boolean beatStarted =      false; int sensor = 0; int sensorValue = 0; int runningSensor = 0; int brightness = 0; uint8_t color = 0; int samples = 20 ; long rmsValue = 0; uint8_t mic_mean = 280; static byte heat[NUM_LEDS]; /////////////////////////////////////////////////////////////////////////////////////////// void setup() {  // sanity check delay - allows reprogramming if accidently blowing power w/leds  delay(3000);  FastLED.addLeds<CHIPSET, DATA_PIN>(leds, NUM_LEDS);  LEDS.setBrightness(BRIGHTNESS);  Serial.begin(115200);  delay(100);  Serial.flush();  while ( Serial.available() ) Serial.read(); // this helps to clear out any junk in the UART } // Vocablulary lesson: //   Twinkling - when individual LEDs "ignite" then slowly burn out //   Sparkling - when a whole mess of LEDs "ignite" at the same time then burn out //   Flickering - when a lit led modulates it brightness to simulate the flicker of a flame void loop() {  // Wait for something in the serial monitor before "Sparkling" the first time.  // This lets you time the sparkle to a particular beat in music.  // In practice, just type a letter into the serial monitor and press enter  // when you want the first sparkle to start.  if (loops == 0 && !Serial.available()) {    nextBeat = millis();  }  else {    if (loops == 0 && beatStarted == false) {      nextBeat = millis();      beatStarted == true;      Sparkle();    }    else {      long deltaTimeSparkle = millis() - nextBeat;      if ( deltaTimeSparkle > 0 ) Sparkle(); // if more time than    }  }  deltaTimeTwinkle = millis() - nextTwinkle;  if ( deltaTimeTwinkle > 0 ) {    Twinkle();  }  FastLED.show(); // display this frame } // This Twinkle subroutine creates a slow "twinkling" of the strip. // It uses the same "heating" methodology as Mark Kriegman's "Fire2012" // where pixels are "heated" and "cooled" and then the tempreature of // each pixel is mapped to a color and brightness. void Twinkle() {  // Step 1. Create a randome number of seeds  random16_add_entropy( random()); //random8() isn't very random, so this mixes things up a bit  seeds = random8(10, NUM_LEDS - 10);  // Step 2. "Cool" down every location on the strip a little  for ( int i = 0; i < NUM_LEDS; i++) {    heat[i] = qsub8( heat[i], COOLING);  }  // Step 3. Make the seeds into heat on the string  for ( int j = 0 ; j < seeds ; j++) {    if (abs(analogRead(A5) - 265) > TWINKLING) {      //again, we have to mix things up so the same locations don't always light up      random16_add_entropy( random());      heat[random8(NUM_LEDS)] = random8(50, 255);    }  }  // Step 4. Add some "flicker" to LEDs that are already lit  //         Note: this is most visible in dim LEDs  for ( int k = 0 ; k < NUM_LEDS ; k++ ) {    if (heat[k] > 0 && random8() < FLICKER) {      heat[k] = qadd8(heat[k] , 10);    }  }  // Step 5. Map from heat cells to LED colors  for ( int j = 0; j < NUM_LEDS; j++)  {    leds[j] = TwinkleColor( heat[j] );  }  nextTwinkle += 1000 / FRAMES_PER_SECOND ; // assign the next time Twinkle() should happen } // Sparkle works very much like Twinkle, but with more LEDs lighting up at once void Sparkle() {  // Step 1. Make a random numnber of seeds  seeds = random8(NUM_LEDS - 20 , NUM_LEDS);  // Step 2. Increase the heat at those locations  for ( int i = 0 ; i < seeds ; i++) {    {      int pos = random8(NUM_LEDS);      random16_add_entropy( random());      heat[pos] = random8(50, 255);    }  }  nextBeat += beatInterval; // assign the next time Twinkle() should happen  loops++ ; } //Play with this for different strip colors CHSV TwinkleColor( int temperature) {  CHSV heatcolor;  heatcolor.hue = 60;  heatcolor.saturation = 0;  heatcolor.value = temperature;  return heatcolor; } 

The problem seems to be fixed if I put the functions above loop(), but I have hundreds of legacy programs that won't compile because the functions are below loop() (as suggested in the Arduino Style Guide!). Does anybody know what is going on here?

The Arduino IDE tries to put in "function prototypes" or "forward declarations" for you. Sometimes it doesn't get that exactly right. You should be able to put in function prototypes of your own. Take the declaration of the function and put a copy, without the function body, near the top of the file. For example:

void Twinkle(); void Sparkle(); 

Wow! Thanks, John. This seems to work perfectly.

So now you're a better C / C++ programmer :slight_smile:

Unfortunately the IDE, by trying to be clever, creates sloppy C / C++ programmers :frowning:

If you have many functions, you can place the prototypes in an include file.

At the right top of the editor pane is a little icon with a down arrow. Click and select 'New Tab'.
Between the iditor pane and the output pane the bar will change and you can enter a name for new file; call it e.g. myprototypes.h

You will now have a new tab in the IDE

You can place all prototypes in there and use the below in your sketch.

#include "myprototypes.h" 

That way your sketch will stay a little cleaner.