Connecting Tech Pros Worldwide Help | Site Map

Problems with return statement in function

  #1  
Old October 18th, 2006, 08:15 AM
mosesdinakaran@gmail.com
Guest
 
Posts: 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

  #2  
Old October 18th, 2006, 08:45 AM
Kimmo Laine
Guest
 
Posts: n/a

re: Problems with return statement in function


<mosesdinakaran@gmail.comwrote in message
news:1161156743.321309.53790@k70g2000cwa.googlegro ups.com...
Quote:
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();
Quote:
}
>
}
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)


  #3  
Old October 18th, 2006, 10:05 AM
mosesdinakaran@gmail.com
Guest
 
Posts: n/a

re: Problems with return statement in function


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:
Quote:
<mosesdinakaran@gmail.comwrote in message
news:1161156743.321309.53790@k70g2000cwa.googlegro ups.com...
Quote:
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();
>
Quote:
}

}
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)
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problems with realloc() lancer6238@yahoo.com answers 7 July 22nd, 2007 06:35 PM
Problems with include and duplication function definitions Wescotte answers 14 April 18th, 2006 12:15 AM
the c# return statement John Bailo answers 94 November 22nd, 2005 11:17 AM
the c# return statement John Bailo answers 94 July 21st, 2005 03:31 PM