Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 406

 
RomanRott:

How do I write a function with a variable number of parameters?

You can set some parameters of a function to an initial value (these parameters should be the last ones). When the function is called, these parameters can be set or not, depending on the need. If parameters will not be set explicitly, they will receive initial values.

The second option is overloading. Simply write a function with the same name, only with different types/number of parameters.

 
Artyom Trishkin:

Like what?


I need to pass several parameters to a function, but in different situations I need to pass a different number, known in advance
and then process these parameters in the function

In C, for example, this is implemented like this

int func(int a, int b, ...)
 

Is there any way to put a "Undo" button on the toolbar in MetaEditor?
(the one that implements Ctrl+Z)

 
RomanRott:

I need to pass several parameters to a function, but in different situations I need to pass a different number, known in advance
and then process these parameters in the function

In C, for example, this is implemented like this

int func(int a, int b, ...)

Pass the array and the number of parameters to take from the array

 
STARIJ:

Pass in an array and the number of parameters to take from the array

So the numbers I need to pass are not in order, i.e. a universal one cannot be generated
 
RomanRott:
So the numbers I need to pass are not in order, i.e. a universal one cannot be generated.
At least give me one example which is not abstract.
 
Artyom Trishkin:
At least give me one non-abstract example.

For example:
Depending on the condition(NOT if, but just basically, I write these numbers myself)

a = func(digit, 10, 18, 42); or a = func(digit, 11, 18, 42, 15, 13); or a = func(digit, 5);


double func(int num, ????)
{

return("sum of these parameters"/num);

}

 
RomanRott:

like this:
Depending on the condition(NOT if, but just basically, I write these numbers myself)

a = func(digit, 10, 18, 42); or a = func(digit, 11, 18, 42, 15, 13); or a = func(digit, 5);


double func(int num, ????)
{

return("sum of these parameters"/num);

}

Well then, what first came to mind, and as already suggested here above - pass in function array double, and in function calculate necessary values from values of passed array - it is possible to know both quantity of numbers(array size), and accordingly to calculate their relations. Only the formula will remain the same for any size of the array. If you are going to change the formula too, then just the array won't work for this, you will have to make one more parameter - which will point to the right formula for the calculation. For example, you can pass an enumeration indicating the type of formula (ENUM_TYPE_CALCULATED) and, based on the value of this enumeration, for example CALCULATE_SUMM, just calculate (in this example) the sum of all values of the passed array.

 
RomanRott:
So the numbers I need to pass there are out of order, i.e. a universal one cannot be formed

Here's a full-fledged answer, but no response...

Forum on trading, automated trading systems & strategy testing

Any questions from newbies on MQL4, help and discussion on algorithms and codes

Alexey Kozitsyn, 2018.01.05 11:46

You can set some function parameters to an initial value (these parameters should be the last ones). When you call the function, you will be able to set these parameters or not, depending on the need. If parameters are not set explicitly - they will get initial values.

The second option is overloading. Simply write a function with the same name, only with different types/number of parameters.


 
Alexey Viktorov:

Here's a full-fledged answer, but no response to it...


If the set and number of numbers are not known in advance, there is no function for every new set.

Reason: