params arguments

 

Hi,

 I can't seem to find an example of defining a function with an arbitrary set of parameters, just like Print(...)  is defined.

 How do I go about declaring and then accessing a parameters array?

 

A code example, or a link to a documentation would be greatly appreciated,

Thank  you.

 
RodionPronin:

 I can't seem to find an example of defining a function with an arbitrary set of parameters, just like Print(...)  is defined.

Arbitrary means random, I can't think of many times that a function would be useful with random parameters


 How do I go about declaring and then accessing a parameters array?

 Do you mean that you want to store the parameters in an array and then access the array from a function?

 
GumRai:

Arbitrary means random, I can't think of many times that a function would be useful with random parameters

 Do you mean that you want to store the parameters in an array and then access the array from a function?

GumRai,

I can think of many reasons why I could have an arbitrary set of parameters that I would like to list as an comma separated arguments to a function. Let's not focus on application of it, I'll worry about the use of it... My question is how to define such a function.

here is a C# analogue:

 

public static void UseParams(params int[] list)
{
    for (int i = 0; i < list.Length; i++)
    {
        Console.Write(list[i] + " ");
    }
    Console.WriteLine();
}

 

or if I don't want to explicitly set those arguments to be ints, I would do this:

 

public static void UseParams(params object[] list)
{
    for (int i = 0; i < list.Length; i++)
    {
        Console.Write(list[i] + " ");
    }
    Console.WriteLine();
}

 

I know mql4 is capable of something like that, their built in function Print does exactly that... I just need syntactical help...

 
GumRai:

Arbitrary means random, I can't think of many times that a function would be useful with random parameters

 Do you mean that you want to store the parameters in an array and then access the array from a function?

And arbitrary doesn't mean random. it means that number of parameters and their types are not known at the time of function definition...
 
RodionPronin:

GumRai,

I can think of many reasons why I could have an arbitrary set of parameters that I would like to list as an comma separated arguments to a function. Let's not focus on application of it, I'll worry about the use of it... My question is how to define such a function.

here is a C# analogue:

 

 

or if I don't want to explicitly set those arguments to be ints, I would do this:

 

 

I know mql4 is capable of something like that, their built in function Print does exactly that... I just need syntactical help...

There is no such construction with mql4. The number of parameters is always fixed.

You can either pass an array by reference or use default values.

 

I'm not sure what you are asking

You can reference an array in a function

  double ParameterArray[];
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---
  ArraySetAsSeries(open,false);
  int x;
  static datetime newbar=0;
  if(prev_calculated==0)
     {
     ArrayResize(ParameterArray,rates_total);
     for(x=0;x<rates_total;x++)
        ParameterArray[x]=open[x];
     newbar=Time[0];
     }
  else
  if(newbar!=Time[0])
     {
     newbar=Time[0];
     int as=ArraySize(ParameterArray);
     ArrayResize(ParameterArray,as+1);
     ParameterArray[as]=open[as];
     }
  
  Func(ArraySize(ParameterArray),ParameterArray);
  
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
void Func(int as, double &PA[])
  {
  string cstring="";
  for( int x=1;x<6;x++)
     cstring+=DoubleToStr(PA[as-x],Digits)+"\n";
  Comment(cstring);
  }
 
RodionPronin:

I know mql4 is capable of something like that, their built in function Print does exactly that... I just need syntactical help...

 I imagine that the Print function works something like

  //In main code
  string st=concatenate("Hello");
  Print(st);
  st=concatenate("Hello"," ","How are you");
  Print(st);
  st=concatenate("Hello"," ","How are you"," Today");
  Print(st);

  //Function------------------

string concatenate(string a="",string b="",string c="",string d="")
   {
   string ps=a;
   ps+=b;
   ps+=c;
   ps+=d;
   return(ps);
   }
   
 
RodionPronin:
And arbitrary doesn't mean random. it means that number of parameters and their types are not known at the time of function definition...

mql4 knows now (b600+) to 'overload functions'.

Ok, this is not really arbitrary (like C) but it give you more flexibility:

// stupid example
int max(int a, int b) {return(fmax(a,b));}
int max(int a, int b, int c) {return(fmax(a,fmax(b,c));}
int max(int a, int b, int c, int d) {return(fmax(fmax(a,b),fmax(c,d)));}
int max(int a, int b, int c, int d, int e) {return(fmax(fmax(fmax(a,b),fmax(c,d)),e));}
double max(double a, double b) {return(fmax(a,b));}
double max(double arr[]) { ...; return(arr[i]); }
...
// no warrenty: for correct bracktes seetings - not tested!
 
A compiler parses the multi arguments functions such as Print or FileWrite using arrays. In MQL you can't define a multi argument function yourself, but you can use a array directly as suggest by Gumray!
 

Gentlemen,

Thank you,  I guess I'll have to cope with either array or string concats in place... 

Reason: