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
17k6 gold badges52 silver badges84 bronze badges
asked Oct 7, 2012 at 14:53
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 );
answered Oct 7, 2012 at 14:54
6
  • sir, but if i change the '>=' condition of if block to '>' , then is there any need of return in the if block Commented Oct 7, 2012 at 15:29
  • @sqlchild what do you mean? I'm not asking you to change it. Commented Oct 7, 2012 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. Commented Oct 7, 2012 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. Commented Oct 7, 2012 at 16:55
  • 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. Commented Oct 11, 2012 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.

answered Oct 7, 2012 at 14:55
2
  • 2
    Was about to post exactly this :) Commented Oct 7, 2012 at 14:57
  • @codaddict : sir, but if i change the '>=' condition of if block to '>' , then is there any need of return in the if block Commented Oct 7, 2012 at 15:28

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.