get value from function return(value) mql4

 

Hello Respected Experts i need your little help can somebody tell me how we can get a value from a function return. here is my example.


//StartFunction Execute always
int start()
{  
//Below nHighPrice Variable calling Hlevel(30,0) Function and getting double value.
 double nHighPrice = Hlevel(30,1);
   
Comment("ReturnIndex: ",index);
   return(0);
  }
//+------------------------------------------------------------------+
double Hlevel(int toBar, int fromBar)
{
        bool status;
        int index = iHighest(_Symbol,_Period,MODE_HIGH,toBar,fromBar);
        if(High[index]>Bid)
        {
         status = true;
        }else
         {
          status = false;
         } 
   
   return(status);
}

is it possible to get the status value from a return(status).

or

double Hlevel(int toBar, int fromBar)
{
        
        int index = iHighest(_Symbol,_Period,MODE_HIGH,toBar,fromBar);
        
   return(index);
}

if i use a int variable in start function and call to Hlevel(toBar,fromBar) then it is getting the number of Highest Bar. 

but my question is where is the return(index) and how we can get in other function variable.

 

If a function returns a value, then you can assign it to a variable or check the value in an if-statement (or a for-loop, while-loop or do-while loop, but this is rare and usually not the best way to go)

Regardless, the value of a function that returns a value will always return the value, whether you assigned it to a variable or what not.

BTW, change start() to OnStart() and make sure you're using #property strict. You can read more about the differences here.

#property strict

int OnStart()

----

In the following function, the function definition of Hlevel returns a double value, but you're actually returning a boolean value.

double Hlevel(int toBar, int fromBar)
{
        bool status;
        int index = iHighest(_Symbol,_Period,MODE_HIGH,toBar,fromBar);
        if(High[index]>Bid)
        {
         status = true;
        }else
         {
          status = false;
         } 
   
   return(status);
}

Depending on what you're going for, you should change it to reflect the correct return value. From what you're doing inside the function, it seems you would want to change double to bool.

----

The same thing happens here. The function definition states that it should return a double value, but you're actually returning an int value.

double Hlevel(int toBar, int fromBar)
{
        
        int index = iHighest(_Symbol,_Period,MODE_HIGH,toBar,fromBar);
        
   return(index);
}

As before, from what you're doing inside the function, change double to int

----

if i use a int variable in start function and call to Hlevel(toBar,fromBar) then it is getting the number of Highest Bar. 

but my question is where is the return(index) and how we can get in other function variable.

Because you're essentially calling iHighest with MODE_HIGH, it's returning the index of the candle with the Highest (iHighest) High (MODE_HIGH) for that particular range from the start candle (fromBar) and the count (toBar) you specified, which is the value of return(index).

Once you have the index, you can extract information about the bar, such as the High, Low, etc. using High[index], Low[index], etc.

Side note: I'd personally rename "toBar" to "count" to be clear about how the function actually works.

Also, iHighest/iLowest counts from right-to-left. Just something to keep in mind if you're not familiar with it.

Updated MQL4 - Language Basics - MQL4 Reference
Updated MQL4 - Language Basics - MQL4 Reference
  • docs.mql4.com
Updated MQL4 - Language Basics - MQL4 Reference