Sorry, i am posting this question a second time. The first was not well arranged and i can’t find a way to delete it.
By my previous understanding, due to the order of execution of recursive functions, when it is called it does not go on to execute the remaining part of the code but creates a new instance of the function in memory, the execution of the rest part of the function occurs at the last instance when the base case is fulfilled. In that light the code snippet
[php]
$row++;
}
// call display on each of this node’s children
// note a node will only have children in its list if expanded
$num_children = sizeof($this->m_childlist);
for($i = 0; $i<$num_children; $i++)
{
$row = $this->m_childlist[$i]->display($row, $sublist);
}
return $row;
}
[/php]
will create several instances of the display() function which display the tree, but only return $row at last instance which is in turn returns $row to the instance that calls it until it get to the first. But the writer of the book says the snippet of code sends $row from one call to the next and then the next. How is this possible when the return of $row occurs after the call to the recursive function. What am I getting wrong?
The entire class code is shown below
[php]
Well, each “instance” of the routine is executed in order. Therefore, changes to $row is done orderly.
Any variables inside the routine are handled in order of the called routine. Therefore, each recursion of
the routine is handled in order. This is nothing like “thread” handling, just a set calling order, one after
another. Read all of the sites I mentioned in the other post. They may help you understand better.
Thanks Ernie. My problem is the “return $row” that occurs after the call to the recursive function
- Is this part of the code executed in each instance of the function, thus returning the value of the row to $row.
- If it executes each times, is it not against the structure of recursive functions; Normally, the part before the call to the recursive function is executed first for all instances, then since the base case fails in the last instance, the rest part is executed for each instance until control is returned to the first calling function.
- If it’s not executed each time then when.
Please a little further help will be appreciated.