skipping parameters in afunction call

 

Anyone know how to create functions where you can skip parameters in the call, that dont need to be changed, something like:

myfunction(1,2,,,5)

//asuming the skipped function parameters are already intitialized ... 

void myfucntion(int param1=1, int param2=2, int param3=3, int param4=4, int param5=5)
{

}

I know I've seen this done but I cant figure out how to do it without getting a "parameter missed" error in the compiler.

 
SDC:

Anyone know how to create functions where you can skip parameters in the call, that dont need to be changed, something like:

I know I've seen this done but I cant figure out how to do it without getting a "parameter missed" error in the compiler.

As far as I am aware you can only skip parameters from the left to right inclusive . . . so if you need to skip the 2nd parameter you also need to skip the 3rd, 4th and 5th, if you don't want to skip the 5th you have to specify them all.
 

Yes thats what I have always done, but I find myself rearranging parameters to avoid long function calls. I think it would be better if,,,, were to mean skipped parameters, use default values. After all, the compiler already knows the parameter is missing because it makes an error to say so, so why cant it just take the default if it is already initialized ?

 
SDC:

Yes thats what I have always done, but I find myself rearranging parameters to avoid long function calls. I think it would be better if,,,, were to mean skipped parameters, use default values. After all, the compiler already knows the parameter is missing because it makes an error to say so, so why cant it just take the default if it is already initialized ?


Wouldn't it be more practical to pass zero and work it out inside the function?

You only need to do it once. Just a thought.

myfunction(1,0,0,0,5)

//asuming the skipped function parameters are already intitialized ... 

void myfucntion(int param1=1, int param2=2, int param3=3, int param4=4, int param5=5)
{
int param_1=1;
if(param1 != 0) param_1 = param1;
}
 

try to overload function.

void myfucntion(int param1, int param2=2, int param3=3, int param4=4, int param5=5)
{

}


void myfucntion(int param1, int param2=2, int param3=3, int param4=4)
{

}

void myfucntion(int param1, int param2=2, int param3=3)
{

}
 

This way is also possible:

class MyClass {
public:
   int param1, param2, param3, param4, param5;
   MyClass() : param1(1), param2(2), param3(3), param4(4), param5(5) {}
   void myFunction() {
      // do something
   }
};

void testFunction() {
   MyClass a;
   a.param5 = 1;
   a.param4 = 1;
   a.myFunction();
}
Reason: