A recursive function is such a function that calls itself until a certain condition is satisfied. In PHP, it is possible to defines a recursive function.
Recursion is used when a certain problem is defined in terms of itself.
Sometimes, it can be tedious to solve a problem using iterative approach. Recursive approach provides a very concise solution to seemingly complex problems.
Recursion in PHP is very similar to the one in C and C++.
Recursive functions are particularly used in traversing nested data structures, and searching or sorting algorithms.
Binary tree traversal, heap sort and finding shortest route are some of the cases where recursion is used.
Calculation of Factorial using Recursion
The most popular example of recursion is calculation of factorial. Mathematically factorial is defined as −
Comments
Post a Comment