473,379 Members | 1,201 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 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 1893
<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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.