Help with overloaded functions

I have an Adafruit NeoPixel ring that I am trying to make a function for. I want to be able to set the pixel, and have the OPTION of setting the color. If it's not set in the function, it will go to some default value (white for example). Something like this:

setLedDir(int led, int r, int g, int b);

where I can either put in the direction and color:

setLedDir(11, 255, 0, 0); // make LED #11 turn red.

... or just set the direction:

setLedDir(11); // make LED#11 turn white (the default value set by the function).

This is what I have now:

#include <Adafruit_NeoPixel.h>

#define PIN 6

Adafruit_NeoPixel ring = Adafruit_NeoPixel(16, PIN, NEO_GRB + NEO_KHZ800);

void setup(){
  ring.begin();
  ring.show();
}

void loop(){  
  // The "on" led stopping in each quadrant for 0.5 seconds:
  setLedDir(0);
  delay(500);
  setLedDir(3);
  delay(500);
  setLedDir(7);
  delay(500);
  setLedDir(11);
  delay(500);
}

void setLedDir(int led, int r, int g, int b){
  ring.clear();
  ring.setPixelColor(led, r, g, b);
  ring.show();
}

I get this error with this sketch:

This report would have more information with
"Show verbose output during compilation"
enabled in File > Preferences.
Arduino: 1.0.6 (Windows NT (unknown)), Board: "Adafruit Flora"
sketch_mar24b.ino: In function 'void loop()':
sketch_mar24b:8: error: too few arguments to function 'void setLedDir(int, int, int, int)'
sketch_mar24b:14: error: at this point in file
sketch_mar24b:8: error: too few arguments to function 'void setLedDir(int, int, int, int)'
sketch_mar24b:16: error: at this point in file
sketch_mar24b:8: error: too few arguments to function 'void setLedDir(int, int, int, int)'
sketch_mar24b:18: error: at this point in file
sketch_mar24b:8: error: too few arguments to function 'void setLedDir(int, int, int, int)'
sketch_mar24b:20: error: at this point in file

You can't overload functions. You can overload the methods of a class.

Mark

Who said you can't overload functions ? Of course you can.

The problem would seem to be, that the OP hasn't actually written one.

Maybe I shouldn't have put "overloaded" in the title, since I'm not 100% sure what it means (anyone care to elaborate). All I would like to know is how to have optional parameters for this function.

You can have optional parameters, or you can have an alternative function which has the same name.

Functions need to be specified before you try to use them. That means, your function should be specified in the code before the loop( ) function. The Arduino compiler has some scam to get around this, which you should not rely on.

I'd write this:

void setLedDir(int led, int r, int g, int b){
  ring.clear();
  ring.setPixelColor(led, r, g, b);
  ring.show();
}

void setLedDir( int led )
{
    setLedDir( led, 255,255,255 );
}

void setup( )
{

}

void loop( )
{

}

michinyon:
You can have optional parameters, or you can have an alternative function which has the same name.

Wow that worked great!! I'm sure I'll be using this a lot more in the future. I'll also be placing functions before the void setup() function from now on. I've got it running with this code now:

#include <Adafruit_NeoPixel.h>
#define PIN 6

Adafruit_NeoPixel ring = Adafruit_NeoPixel(16, PIN, NEO_GRB + NEO_KHZ800);

void setLedDir(int led, int r, int g, int b){
  ring.clear();
  ring.setPixelColor(led, r, g, b);
  ring.show();
}

void setLedDir(int led){
  setLedDir(led, 255,255,255);
}

void setup(){
  ring.begin();
  ring.show();
}

void loop(){  
  // The "on" led stopping in each quadrant for 0.5 seconds:
  setLedDir(0);
  delay(500);
  setLedDir(3, 255, 0, 0);
  delay(500);
  setLedDir(7, 0, 255, 0);
  delay(500);
  setLedDir(11, 0, 0, 255);
  delay(500);
}

It would read better with the RGB values in arrays, you create colours like this:

const int red[] = {255, 0, 0}
const int fuschia[] = {230, 32, 112}
// etc...

Call it with:

setLedDir(4, red);
setLedDir(9, fuschia);
// etc...

The function definition looks like:

void setLedDir(int led, int colour[])
// etc...