By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,181 Members | 1,041 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,181 IT Pros & Developers. It's quick & easy.

Problems with return statement in function

P: n/a
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();
}

}
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........
To my surprise at some rare condition I can get the value in
function one() all the othe times the return value is empty I could not
find this rare condition also.

I have spend a lot of time to find what is happening but in
vain..........

What I need is..

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()
Can any body please help..............

regards
moses

Oct 18 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
<mo************@gmail.comwrote in message
news:11*********************@k70g2000cwa.googlegro ups.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
sp**@outolempi.net | rot13(xv***@bhgbyrzcv.arg)
Oct 18 '06 #2

P: n/a
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:
<mo************@gmail.comwrote in message
news:11*********************@k70g2000cwa.googlegro ups.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
sp**@outolempi.net | rot13(xv***@bhgbyrzcv.arg)
Oct 18 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.