Function(Color = "Red", Speed = "Fast")

Does anyone know the name and if this is possible for the method of defining multiple arguments/parameters in a function inside a class where you can specify multiple definable parameters for example i want a function in my class like this.

Lights.Config(Color = "Red", Speed = "Fast", Brightness = 255);

but to be able to choose some or part of the parameters so another way is if i had 5 options to set and i wanted to be able to only set 1 and leave the rest as they are can this be done?

Thanks i hope this makes sense.

consider

void
str (
    const char *a = "red",
    const char *b = "fast",
    const char *c = "hot" )
{
    printf (" %s, %s, %s\n", a, b, c);
}

You can define the function like this

void myFunction (byte brightness) {

for a single variable and like this for the three variables you mention (hope I have the syntax correct)

void myFunction ( char * color, char * speed, byte brightness) {

However you will make life a great deal easier if you just use a single character for the parameters color and speed. For example use 'R' for red and 'F' for fast. Then the function will be

void myFunction ( char color, char speed, byte brightness) {

and (more importantly) the code to interpret the function will be very much simpler - for example

if (color == 'R') {

rather than

if (strcmp(color, "Red") == 0) {

...R

PS ... (following Reply #2) the way to call the function would be

myFunction("Red", "Fast", 255)

or, for my preferred way

myFunction('R', 'F', 255)

No, setting parameters by name is a python thing. It doesn't happen in C++. Sorry but you'll have to think of another way.

C++ allows default arguments (see above)

IF you are asking about default values for function arguments, if do NOT specify values for all arguments, the defaults will be used for the one you do not specify. But, if you want to specify one value, you MUST specify values for ALL preceding arguments. You cannot specify only one in the middle of the argument list.

Lights.Config(Color = "Red", Speed = "Fast", Brightness = 255)
{
}

...

Lights.Config("Blue");                     // Arguments are "Blue", "Fast", 255
Lights.Config("Blue", "Slow");          // Arguments are "Blue", "Slow", 255
Lights.Config("Blue", "Slow", 128);  // Arguments are "Blue", "Slow", 128
Lights.Config("Slow");                    // Arguments are "Slow", "Fast", 255
Lights.Config(128);                        // NOT valid

Regards,
Ray L.

Thank you all for your help and the advice on single letter parameters is great but im still not sure im understanding the examples posted.

So if i have 3 arguments for my function and i want to declare argument 1, 2, 3 or just argument 1 and 3 how would this be done.

So we have 3 arguments color, speed and say brightness.

my header file would have like so.

void Classname::Config(char color, char speed, int brightness);

now if i want to just set the color and brightness or speed and brightness it would no doubt return an error could someone show me any implementation of this or tell me if i am not making sense.

i have written a test function below:

code.h

#ifndef CP
#define CP

#if (ARDUINO >= 100)
  #include "Arduino.h"
#else
  #include "WProgram.h"
#endif

class my_class{

   public:
   
   my_class();
   
   void config(char arg1, char arg2, int arg3);


};

#endif

code.cpp

#include "code.h"

my_class::my_class(){
//Nothing.
}

my_class::config(char arg1, char arg2, int arg3){


    if(arg1 != ''){
    Serial.println(arg1);
    }

    if(arg2 != ''){
    Serial.println(arg1);
    }

    if(arg3 != 0){
    Serial.println(arg1);
    }
}

code.ino

#include "code.h"

my_class my_class;

void setup(){
    my_class.config('r', 's',  255); //Argument 1, 2 & 3  //
    my_class.config('r', 's'); //Argument 1 & 2              // - all can be done by default value
    my_class.config('r'); //Argument 1                         //
    my_class.config('s', 255); //Argument 2 & 3 Is this possible
    my_class.config(255); //Argument 3, is this possible.
}

I may have some errors in the code i have just typed this in a comments box of memory so please excuse if you spot anything, hope this explains better.

void
str (
    const char *a = "red",
    const char *b = "fast",
    const char *c = "hot" )
{
    printf (" %s, %s, %s\n", a, b, c);
}

int
main ()
{
    str ();
    str ("blue");
    str ("blue", "slow");
    str ("blue", "slow", "cold");
}
red, fast, hot
 blue, fast, hot
 blue, slow, hot
 blue, slow, cold

But, if you want to specify one value, you MUST specify values for ALL preceding arguments. You cannot specify only one in the middle of the argument list.

Is the above NOT clear enough??

If you want to specify argument 2, you MUST specify argument 1. If you want to specify argument 3, you MUST specify BOTH arguments 1 AND 2. You CANNOT pick and choose which ones you specify.

Regards,
Ray L.

Sorry for not being clear, my application may help in which i have several variables to change for an led ring but upon changing those i want some code associated to transition the new variables into effect and i wanted to avoid having to write 6 or so individual functions so that i could change 1,2,3 or even all the variables.

My idea was i could have a config function in my class in which i specify a interchangeable amount of parameters depending upon what i need to change allowing me to reuse that function throughout the entire code hence why i thought about the python keyword method and wondered if an implantation of something similar existed in C++.

gcjr has written something i am currently looking at trying to figure out how that will or would work.

The simple solution to this dilemma is to define the variables as globals and don't bother passing parameters.

...R

One way to do something similar is to make a function that takes a variable number of arguments and use an enum to label each argument. Since the number of arguments is unknown you will need to add an end marker.

enum argtypes {Color, Speed, Brightness, End};


void Config(enum argtypes type = End, ...)
{
  // Set defaults
  const char *color = "Red";
  const char *speed = "Fast";
  int brightness = 255;

  va_list args;
  va_start(args, type);

  while (type != End)
  {
    switch (type)
    {
      case Color: color = va_arg(args, char *); break;
      case Speed: speed =  va_arg(args, char *); break;
      case Brightness: brightness = va_arg(args, int); break;
      case End: break;  // Just here to avoid "'End' not handled" warning
    }
    type =  (enum argtypes) va_arg(args, int);
  }

  Serial.print("Color=");
  Serial.print(color);

  Serial.print("  Speed=");
  Serial.print(speed);

  Serial.print("  Brightness=");
  Serial.println(brightness);
}

void setup()
{
  Serial.begin(115200);

  Config(Color, "Blue", Speed, "Slow", Brightness, 128, End);  // Specify all
  
  Config(Color, "Orange", End);  // Or specify some
  
  Config(Brightness, 90, Speed, "Medium", End);  // In any order
  
  Config(); // Or specify none and use all defaults
}

void loop() {}

Output:

Color=Blue  Speed=Slow  Brightness=128
Color=Orange  Speed=Fast  Brightness=255
Color=Red  Speed=Medium  Brightness=90
Color=Red  Speed=Fast  Brightness=255