i thought recursion function meant the calling of the function in itself till a particular condition is satisfied, then when the last called function is over or returns, after that it keeps re-winding till it reaches the the fuction which was called first.
well i guess i must be wrong cause i cant understand how this 'binary search' function works?(a function which checks if a particular element exists in an array of numbers tats arranged in ascending order)
/*
x- the element to search for
l- the low limit of the array
h- the high limit
theres an int a[10]; that this function works on
*/
int array::bs(int x , int l , int h){
???if(l<=h)
???{
??????int m;
??????m=(l+h)/2;
??????if( a[m] == x)
?????????return (1);
??????else if( x < a[m] )
?????????h=m-1;
??????else if( x > a[m] )
?????????l=m+1;
??????bs(x,l,h);
???}
???else
??????return (0);
}
ones it reaches a return statement should'nt it go back to the previous function that called it and not back to the main() ???
this one is a fine tail recursive function. It has nothing todo after the recursive call, so the compiler optimizes the call so that only one and the same stackframe is used ever - and no rewinding or returning to previous recursive calls may happen in that case. *chuckle* It isn't that hard to get. Take it as if it were a loop, hm? Just expressed with other means.
Actually it does that, but there's no explicit return statement, which strictly speaking is a bug (more below). Yet, after returning, it just falls to the end of the function, and hence returns.
If you look at the recursive call, you see that if it's taken, then no code in the function really will be executed by the caller once the call returns. This is actually an example of a tail-recursive function; it replaces the problem with a smaller one without revisiting the more general case again; the more general case just returns.
But, the code is not correct. The recursive call should have "return" in front of it. The reason it works (if it works?) is that when a recursive call returns, the function will then return again, without any return value. But since return values are returned in EAX on x86, if you're lucky the function won't change that register after the call returns, and hence the return value from the inner call will remain in the register, causing the code to work. It might fail on some other architecture with a different calling convention though.
Oh, and whoever wrote the code should learn that "return" is not a function, and therefore there's no reason to bother with unnecessary parenthesis around it's value. Really.
this one is a fine tail recursive function. It has nothing todo after the recursive call, so the compiler optimizes the call so that only one and the same stackframe is used ever
it does? are you quite certain? (assuming this is C++ and not scheme or somesuch in disguise)
well, maybe some compilers do. my version of g++ doesn't, for one.
???
i'd guess this implementation would go down the stack, even though it is unnecessary.
well i modified it a little so that depending on what the last called function returns the function that called it will return.(so all the functions called will have to return a value)
You don't need the if statement around the recursive call. Also, using bool as the return type instead of int makes the intent of the function more clear.
btw. you don't need an "else" if the other branch always ends in a return.
less words, less indentation, less options to consider at a given point = easier to read.
if(l<=h)
{
int m=(l+h)/2; // initialize a variable in-place
if( a[m] == x)
return true;
if( x < a[m] ) // else not needed - no other way to get here
h=m-1;
else // if( x > a[m] ) // condition not needed - there is no other option
l=m+1;
return bs(x,l,h);
}
// else // not needed
return false;