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
2
  • 1
    Please take some time properly indent your code next time, that makes it easier for us to comprehend your code. Thank you. Commented Oct 8, 2012 at 14:31
  • sorry for not managing the code Commented Oct 11, 2012 at 10:09

2 Answers 2

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 );
Sign up to request clarification or add additional context in comments.

6 Comments

sir, but if i change the '>=' condition of if block to '>' , then is there any need of return in the if block
@sqlchild what do you mean? I'm not asking you to change it.
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 You need to string together your functions so that they actually return to eachother. If they don't, it won't work.
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.
|
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.

2 Comments

Was about to post exactly this :)
@codaddict : sir, but if i change the '>=' condition of if block to '>' , then is there any need of return in the if block

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.