How can I define a default parameter for a function?

 

Hi guys,

pretty difficult to explain in a headline. I have this function:

double ClosePrice(string symbol, int timeframe, int position) {
   return   iClose(symbol, timeframe, position);
}

And I call this function like this:

double price = ClosePrice("EURUSD", PERIOD_H1, 10);

Now I want to define the default parameter "position" as 1 if there is no position parameter given.

So I want to do this:

double price = ClosePrice("EURUSD", PERIOD_H1);

And the result should be  the closeprice of EURUSD H1 of candle 1.

Is that possible? I know it is when using some MT4 functions.

 

Hi,

So you can do as this :

double ClosePrice(string symbol, int timeframe=0, int position=1) {
   return   iClose(symbol, timeframe, position);
}

"position=1" is for one bar before current bar,And "timeframe=0" is for current chart timeframe,so you can leave them empty during function calling:

double price = ClosePrice("EURUSD");
Regards.
 
Mehrdad Jeddi:

Hi,

So you can do as this :

"position=1" is for one bar before current bar,And "timeframe=0" is for current chart timeframe,so you can leave them empty during function calling:

Regards.

Exactly what I was looking for. Thank you very much!

 
Marbo:

Exactly what I was looking for. Thank you very much!

You're welcome.