Can a function return more than just one value?

 

Hi coders,

I made a simple example of a function within a script. Is it possible to return not only the high but also the low, open and close of the outsidebar? Would it also be possible to return the iBarShift for that bar because this would be an int-value and no double.

Thanks!

double Outsidebar() {
   int i;
   for (i=1; i<Bars; i++) {
      if (High[i]>High[i+1] && Low[i]<Low[i+1])
         break;
   }
   return High[i];
}
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
   Comment(Outsidebar());
  }
//+------------------------------------------------------------------+
 

Hello,

 There are a couple of ways to do what you are after. What I would do would be to pass parameters by reference, see

 https://docs.mql4.com/basis/function/parameterpass 

 By doing so you are able to alter the value of a variable in a "remote" fashion, i.e. the function can not return anything but still , it can proccess and replace the values

of the variables passed to it by reference 

 

void OnStart()
{          
    MessageBox(a+" "+b);  
    
    pass_by_reference(a,b);
    
    MessageBox(a+" "+b);
}

int a=5; double b=5.5;

void pass_by_reference(int &apbr, double &bpbr)
{
    apbr=8; bpbr=8.8;
}

 

 best regards

 

Hello Demos,

this is completely new for me. I never passed parameters by reference. Therefore I don't reallly understand it right now. But I will study the docs.mql4 reference.

Thank you! 

 

Hello, you are welcome :) As a note, I suggest looking for pointers in C language as it is what passing by reference is about; looking at them will presumably be a big help to understand the difference from passing by value. Please pay attention to the fact that MQL has some so-called Object Pointers, which I am not sure what they are about and if they are related in any way to the pointers of C - so I am only referring to pointers in C, in general

 

best regards 

Reason: