Problem with return..

 

Hi,

Could anyone explain me why this function return 0 whereas the Print just before the return instruction says 1 ?

bool resistanceConfirm(int i, int k, double firstPoint, double coeff, bool resistanceConfirm){ 
    static int cpt = 0;
    if(iClose("EURUSD", PERIOD_H4, k) <= firstPoint + (i - k) * coeff) cpt++;
    Print("cpt = " + cpt);
    if(cpt == 3) resistanceConfirm = true;
    Print("resistanceConfirm1 : " + resistanceConfirm);
    if(k > 1) resistanceConfirm(i, k-1, firstPoint, coeff, resistanceConfirm);  
    else {
        k = 3;
        cpt = 0;
        Print("resistanceConfirm2 : " + resistanceConfirm);
        return(resistanceConfirm);
    }  
}
bool resistanceConfirm = false;
resistanceConfirm = resistanceConfirm(i, 3,firstPoint, coeff, resistanceConfirm);
            Print("resistanceConfirm3: " + resistanceConfirm);

And I obtain:

2011.11.12 13:01:42 2011.05.01 23:11 myEA EURUSD,H4: resistanceConfirm3: 0

2011.11.12 13:01:42 2011.05.01 23:11 myEA EURUSD,H4: resistanceConfirm2 : 1

2011.11.12 13:01:42 2011.05.01 23:11 myEA EURUSD,H4: resistanceConfirm1 : 1


 

Either make resistanceConfirm a Global Scope Variable and remove it from the

bool resistanceConfirm(int i, int k, double firstPoint, double coeff, bool resistanceConfirm){ 

Or give them different names. As in bool resistanceConfirm_to_be_passed = false;

The variables within a function is NOT going to keep their value outside of the function (Unless they're Static or Global Scope). There are 2 different resistanceConfirm within your code not 1.

 

Wow . . you like to make life hard for yourself . . and complicated.

To summarise . . . resistanceConfirm is a function, a local variable and maybe a global variable . . . really can't see why you are having problems. Then you call resistanceConfirm recursively . . .

 
Arkenis:

Could anyone explain me why this function return 0 whereas the Print just before the return instruction says 1 ?

Firstly, it's really not a good idea to give variables and functions the same name. It makes things potentially very confusing.

Leaving that aside, what appears to be happening in your code is as follows:

* The resistanceConfirm() function gets called with k = 3

* This triggers the line "if(k > 1) resistanceConfirm(i, k-1, firstPoint, coeff, resistanceConfirm);" and the function therefore does repeated recursive calls to itself with decrementing k values until k = 1.

* During the final recursive call when k = 1, the function logs the "recursiveConfirm2" output

* However, that's then returning not to the original calling code outside the function, but back up a level to the previous recursive call

* Finally, when all the recursive calls are complete, the function will exit without returning any explicit value; its return value therefore defaults to false/0. As a result, if the function is called with k > 1, then it will always return false/0.

In other words, the "if (k > 1) ... else ..." block is effectively saying the following: "If k = 1, then log the value of the resistanceConfirm parameter and simply return it to the caller. But if k > 1, then recursively call the same function, but ignore the return value of the recursive calls and always eventually return false to the calling code".

I'm not sure exactly what you're trying to achieve, but you may have intended to do "if(k > 1) return resistanceConfirm(i, k-1, firstPoint, coeff, resistanceConfirm); "

 
jjc:

[...] I'm not sure exactly what you're trying to achieve, but you may have intended to do "if(k > 1) return resistanceConfirm(i, k-1, firstPoint, coeff, resistanceConfirm); "

... Just clarifying that slightly, your function seems to be behaving the same general way as the following, which will return false for any k value > 1.

bool Test(int k)
{
   if (k > 1)
      Test(k-1);
   else
      return (true);  
}

(All this isn't helped by the fact that the MQL4 compiler doesn't warn you about functions which don't have explicit return values.)

 

If I understand correctly, the function resistanceConfirm() will keep the first call's return value ? Because I thought the last call's return value would overwrite the previous one ..

I tried to correct my code:

int resistanceConfirm(int i, int k, double firstPoint, double coeff){
    static int cpt = 0;
    if(iClose("EURUSD", PERIOD_H4, k) <= firstPoint + (i - k) * coeff) cpt++;
    Print("cpt = " + cpt);
    if(cpt == 3){
        cpt = 0;
        return(1);
    }
    
    else if(k > 1) return(resistanceConfirm(i, k-1, firstPoint, coeff));  
    
    else {
        cpt = 0;
        return(0);
    }  
}

I guess it works correctly, but if you can confirm..