Ok, you still haven't understood how functions and return work.
If the function does not return anything in this case ...it does not return anything, nada, njiente. It does not matter wether a function calls itself or another function. If the function calls itself it's still a function call.
There's something called
call stack. You can put a new item on the stack and you can remove the topmost item. But if you want to remove an item below other items you first have to remove all the items above. That's how function calls work. e.g.
Code: Select all
function foo() {
return 3;
}
function bar() {
$v = foo();
echo 'v: ', $v, "<br />\n";
}
$v = bar();
echo 'v: ', $v, "<br />\n";
when the script's execution reaches the line
return 3; the call stack looks like
foo <- bar <- [main]
foo is still executed. When it's done the script continues with the code in bar() after the call of foo(). When bar() is done [main] continues after the call of bar().
A return value is only available to next topmost item on the call stack. So foo() can only return a value to bar(). If bar() doesn't pass that value to [main] it's lost.
The same is true if bar() calls itself.
Code: Select all
function bar($param) {
if ( 0==$param) {
return 'xyz';
}
else {
bar($param-1);
}
}
$v = bar(1);
echo $v;
When bar() is called the first time ( bar(1) ) the call stack is bar<-[main]. Let's add the parameters to the call stack: bar(1)<-[main]
$param is not 0, therefore the else-block is executed and bar calls itself, this time with the parameter 1-1=0, call stack: bar(0)<-bar(1)<-[main]
Questions: whereto does
return 'xyz'; return the string? What happens next?