472,950 Members | 2,120 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,950 software developers and data experts.

Problems with return statement in function

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
2 1869
<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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Romulus Mare | last post by:
How can I avoid writting and maintaining the same code in the two operator implementation below? I know that in the minimal example below, the implementation is trivial, but I found this type of...
3
by: Mark Morton | last post by:
I'm writing an if statement for a UK credit card form validation script. Users who specify that their card is Switch need to enter either the issue number or the 'valid from' date. I'm trying to...
3
by: Avalon1178 | last post by:
Hi, I recently downloaded the xalan-c source code in http://mirrors.ccs.neu.edu/Apache/dist/xml/xalan-c I followed the instructions from the apache site on how to build it (I already have...
4
by: bob garbados | last post by:
I'm new to web services and I'm trying to interface with a payment gateway for an online store. I'm trying to do this without Visual Studio and I'm stuck... I created my proxy class from the...
0
by: Tim::.. | last post by:
Can someone please help! I'm trying to write an insert statement for a complex datagrid! The database consists of the following data structure! ..tblContent PageID PK ModDate Description...
21
by: matvdl | last post by:
I have a system that was originally developed in asp - the pages are saved in SQL (there are over 10,000 pages) and saved to a temp directory in the server when requested by a client. I have...
14
by: Wescotte | last post by:
I have an application that uses several file formats for similar data. So I've created various php files for each format containing the same functions which produce the same end result. Now I...
6
by: Trev17 | last post by:
Hello, I am trying to write a program that reads in a file, and uses a 2d array to store the highest and lowest temp from each month. It also outputs the average high and the average low. The file...
6
by: kevin | last post by:
Hi All here is a problem I just can't understand! I don't really want a work around I just want this to plainly work or atleast explained.... Thanks in advance.... Problem: strcmp(st, s) it runs...
2
by: chromis | last post by:
Hi there, I've been reading an OOP book recently and it gives some nice Adaptor / Template patttern code to wrap around the php Mysql functions. I thought that I'd try and create a Simple Address...
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.