1

I am using recursion to decrease the value of a number and equalize it to another number, but the result yielded is blank and I can't find the error.

$A = 40;
$B = 130 ;

function Equalize( $A , $B ) {
    if (   $B  -   $A   >= 30 ) {
        $Start = $A + 30 ;
        Equalize($Start , $B );
    }
    else {
       //if I place- echo 'A='.$A; here;
       // then it echoes : A=130.but **return** doesn't works....???       
       return $A;        
    }
}

$Result  = Equalize( $A , $B );
 echo 'Final Result ='.$Result ; //here it shows result as empty

Update

$A = 40;
$B = 130 ;

function Equalize( $A , $B ) {
    if (   $B  -   $A   > 30 ) {
        $Start = $A + 30 ;
        Equalize($Start , $B ); **DO I NEED 'return' HERE TOO before function call ????**   
    }         
    else {
        //if I place- echo 'A='.$A; here;
        // then it echoes : A=100.but **return** doesn't works....???
        return $A;            
    }
}       
$Result  = Equalize( $A , $B );
echo 'Final Result ='.$Result ; //here it shows result as empty
phant0m
  • 16,595
  • 5
  • 50
  • 82
sqlchild
  • 8,754
  • 28
  • 105
  • 167

2 Answers2

3

In your first calling of Equalize($Start , $B ); within Equalize, make it return the calling of the Equalize function instead of just calling it.

 $Start = $A + 30;
 return Equalize($Start , $B );
jeremy
  • 9,965
  • 4
  • 39
  • 59
  • sir, but if i change the '>=' condition of if block to '>' , then is there any need of return in the if block – sqlchild Oct 07 '12 at 15:29
  • @sqlchild what do you mean? I'm not asking you to change it. – jeremy Oct 07 '12 at 15:32
  • no no , i also have another function in which in thie IF clause in place of '>=' i have '>' and am just using that to decrease a value. so , then in the if clause do i need to put 'return' before the function call. – sqlchild Oct 07 '12 at 16:42
  • 1
    @sqlchild You need to string together your functions so that they actually return to eachother. If they don't, it won't work. – jeremy Oct 07 '12 at 16:55
  • but why do i need a return ? the if condition is not satisfied at the last and it goes to else and there i have mentioned return – sqlchild Oct 11 '12 at 10:08
  • 1
    Think of it like a chain. Say you want to use a factorial function, f(x). Because f(x) subtracts one, multiplies and calls itself, in order for the first call of f(x), f(x)1 to get f(x)2's multiplied value, it needs to return it. In other words, how will the first function know what the last function (which could be 100 calls later) returns? I mean, obviously it returns `$A`, but it's only returning `$A` to the last function that called it. If you don't understand it, don't worry about it. Just know that with any recursive function, you use f(x) if you're trying to string together values. – jeremy Oct 11 '12 at 11:34
2

In your if block change

Equalize($Start , $B );

to

return Equalize($Start , $B );

because you are not explicitly returning anything from the if block and as a result a null gets returned.

Community
  • 1
  • 1
codaddict
  • 445,704
  • 82
  • 492
  • 529