Scope of a Variable in Recursive Function

Scope of a Variable in  Recursive Function

Scope of a Variable in Recursive Function

A recursive function is one that calls itself, often over and over again. These are sometimes used to build tree views when data is hierarchical or for calculating some values using some procedure or logic. When calling the same function over and over again, we’re reusing the same variable names. But Its does as follows.

<?php
function recursive($count)
{
if($count==0)
exit;

if($count % 2==0)
{
echo “Even :”.$count . “<br>”;
recursive(–$count);
}
else
{
echo “Odd :”.$count . “<br>”;
recursive(–$count);

}
}
recursive(10);
?>

At Each execution of a function as a self-contained entity in memory. Even when called from within itself, that new call makes a copy of itself and any variable declarations made within that function stay within it.

Leave a Reply

You must be logged in to post a comment.


All material @ copyrighted by chrisranjana.com. If you want to link to this article you are welcome to do so. Unauthorized publication is strictly prohibited. This developer tutorial website contains articles by Php programmers , Software developers, Mysql programmers and asp c# programmers. This website also contains ajax tutorials and advanced mysql sql stored procedures and functions tutorials and sample codes.