Download MetaTrader 5

skipping parameters in afunction call

To add comments, please log in or register
Do you want to write a trading robot? Find solution in MQL5 Reference!
Ian Venner
2584
Ian Venner 2014.03.02 23:12 

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.

Simon Gniadkowski
Moderator
18001
Simon Gniadkowski 2014.03.02 23:20  
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.
Ian Venner
2584
Ian Venner 2014.03.02 23:36  

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 ?

Daniel Petrovai
440
Daniel Petrovai 2014.03.05 04:34  
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;
}
Ziheng Zhuang
1524
Ziheng Zhuang 2014.03.05 06:30  

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)
{

}
Ex Ovo Omnia
3151
Ex Ovo Omnia 2014.03.05 08:41  

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();
}
To add comments, please log in or register