Regarding to return function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
desmondlk
Forum Commoner
Posts: 27
Joined: Tue Sep 24, 2002 10:27 pm
Location: Malaysia

Regarding to return function

Post by desmondlk »

Dear all,

I have encountered a problem regarding to how to return the value back to the calling function.

This is the code that i used to test the problem.

Code: Select all

<?php
function test ($num, $cnt)
{
    if($cnt == $num){
        return $cnt;
    }
    else{
        $cnt+=1;
        test ($num, $cnt);
    }
}
$str = test (4, 3);
echo $str;

?>
The "$str" is empty. It does not pass the value back to the calling function.

However, if i comment this line "test ($num, $cnt);", it returns the value back to the calling function.
The "$str" is 4.

Code: Select all

<?php
function test ($num, $cnt)
{
    if($cnt == $num){
        return $cnt;
    }
    else{
        $cnt+=1;
        //test ($num, $cnt);
    }
}
$str = test (4, 4);
echo $str;

?>
Please help. Thanks in advance.
Gleeb
Forum Commoner
Posts: 87
Joined: Tue May 13, 2003 7:01 am
Location: UK
Contact:

Re: Regarding to return function

Post by Gleeb »

Code: Select all

<?php
function test ($num, $cnt)
{
    if($cnt == $num){
        return $cnt;
    }
    else{
        $cnt+=1;
        test ($num, $cnt);
    }
}
$str = test (4, 3);
echo $str;

?>
becomes

Code: Select all

<?php
function test ($num, $cnt)
{
    if($cnt == $num){
        return $cnt;
    }
    else{
        $cnt+=1;
        return test ($num, $cnt);
    }
}
$str = test (4, 3);
echo $str;

?>
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

funny, either this is some kind of joke or you have a real bad testing day ;)
let's take the first code-snippet and $str = test (4, 3);
  • $cnt != $num so the else-block is executed
  • $cnt+=1; // $cnt == 4
  • test($cnt, $num); // test(4,4)
  • now $cnt == $num
  • return $cnt;
  • but the "outer" function does not return this value
  • nothing's assigned to $str

$str = test (4, 4);
  • $cnt == $num
  • return $cnt; // return 4
  • $str = 4;
try

Code: Select all

<?php
function test($num, $cnt)
{
    if($cnt == $num){
        return $cnt;
    }
    else{
        $cnt+=1;
        return test($num, $cnt);
    }
}
$str = test(4, 3);
echo $str;

?>
edit:
:oops:
<-- way too slow
Gleeb
Forum Commoner
Posts: 87
Joined: Tue May 13, 2003 7:01 am
Location: UK
Contact:

Post by Gleeb »

Don't worry, you know what they say... Great Minds Think Alike...

At least you explained it ;)
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

today a can blame .NET studio :]
it seems to be clueless about the errors it encounters when combined with STLPort. It's more like Guess-Your-Fault ;)
Post Reply