|
Posted by mosesdinakaran@gmail.com on 10/18/06 09:17
Hi Kimmo
Thanks for ur help
I have learned a great lesson from u........
Always return a recursive funtion call
regards
moses
Kimmo Laine wrote:
> <mosesdinakaran@gmail.com> wrote in message
> news:1161156743.321309.53790@k70g2000cwa.googlegroups.com...
> > Hi everybody,
> >
> > Today I faced a problem where I am very confused and I could not
> > solve it and I am posting here....
> >
> > My question is
> > Is is possible to return a value to a particular function
> >
> > The question may be silly or even meaning less but
> > please............
> >
> > Below I have give a detailed description on why this question
> > appears to me.
> >
> > Consider the following Code.
> >
> > function one()
> > {
> > $ret_val = three();
> > echo "<br>RetVal:=".$ret_val;
> > }
> > function two($a,$b)
> > {
> > echo "<br>I am In function ".__FUNCTION__."()";
> > if($a==$b)
> > return true;
> > else
> > return false;
> > }
> > function three()
> > {
> > $array_one= array(1,2,3,4,5);
> > $array_two= array(1,2,3,4,5);
> > $a = array_rand($array_one,1);
> > $b = array_rand($array_two,1);
> > if(two($a,$b))
> > {
> > $ret_val = "$a and $b are equal";
> > echo "<br><br>I am In function ".__FUNCTION__."() and the
> > Val is $ret_val<br>";
> > return $ret_val;
> > }
> > else
> > {
> > three();
>
> NOTE: This should be: return three();
>
> > }
> >
> > }
> > one();
> >
> > Whats happpening here is.
> >
> > 1) I have three functions named one() two() and three();
> > 2) From the first function one() I am calling function three()
> > 3) In function three I have two arrays named $array_one and
> > $array_two
> > 4) Then I am getting a random value from this two arrays as $a,$b
> > 5) Then the function two is called to check weathe this two
> > random value is equal or not if the values are equal it returns true
> > else false
> > 6) Function three is called recursly till the random value $a
> > and $b are equal. If it is equal I need to return the two values to
> > function one()
> > 7) At one time $a and $b will be equal at this moment I return a
> > variable $ret_val from function three() to function one()
> > 8) So In function one I can get the $ret_val
> >
> > But I could not get the value $ret_val in function one()
> > I dont know why........
> >
> > I should return the value $ret_val from function three() to
> > function one() But it is not happening I dont know weather the value
> > has been returned to function three() or to function two()
>
>
> When you call three() recursively, you're not returning the value there.
> When you call two in the other branch, you are returning the value there,
> but it's lost since you've broken the chain of returns when calling three.
> It goes something like this:
>
> one() calls three(), prints what three() returns
> three() calls three(), returns nothing
> three() calls three(), returns nothing
> three() calls three(), returns nothing
> three() calls three(), returns nothing
> ... at some point random matches and...
> three() calls two(), returns $retval
>
>
> It should be
> one() calls three, prints what three() returns
> three() calls three(), returns what three() returns
> three() calls three(), returns what three() returns
> ...
> three() calls two(), returns $retval
>
> so that $retval can make it back to one(). Kudos on using recursion, you
> just missed a tiny little detail... Always return a recursive funtion call.
>
> --
> "Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpk
> http://outolempi.net/ahdistus/ - Satunnaisesti päivittyvä nettisarjis
> spam@outolempi.net | rot13(xvzzb@bhgbyrzcv.arg)
Navigation:
[Reply to this message]
|