Can a function return more than just one value?

Back to topics list To post a new topic, please log in or register
avatar
372
mar 2015.05.12 22:20
 

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());
  }
//+------------------------------------------------------------------+

 

avatar
126
Demos 2015.05.12 23:02 #
 

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

avatar
372
mar 2015.05.13 08:05 #
 

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! 

avatar
126
Demos 2015.05.13 12:38 #
 

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 

Back to topics list  

To add comments, please log in or register