• Register

The programmer must ensure that the recursive function does not become an endless loop.

What is the recursive function?

A recursive function can be defined as a routine that calls itself directly or indirectly during its execution. This enables the function to repeat itself several times and display the output at the end of each loop.


function Count (integer N)

    if (N <= 0) return "Must be a Positive Integer";

    if (N > 9) return "Counting Completed";

    else return Count (N+1);

 end function

The function count() uses recursion to count from any number between 1 and 9.

Why we use a recursive function?

Recursive functions are used in computer science because they allow the programmers to write efficient programs using a minimal amount of code. The downside is that they can cause infinite loops and other unexpected results if not handled properly.


For example, in the above program, the function is terminated if the number is 0 or less or greater than 9. If the proper cases are not included in the function to stop the execution then the recursion will repeat forever that will be causing the program to crash or worse than crash and then hang the entire system.

posted Jul 1 in php 3,890 points