Recursion function is a function which is call itself. In the given example, we have called the recursion function to print the prime numbers from 10 to 1. A recursion function continues until some condition is met to prevent it. That's why, we use the if statement to break the infinite recursion.
<?php
function countdown($num_arg)
{
if($num_arg > 0)
{
print("Number is $num_arg<br>");
countdown($num_arg - 1);
}
}
countdown(10);
?>
Output of the above code
Number is10
Number is9
Number is8
Number is7
Number is6
Number is5
Number is4
Number is3
Number is2
Number is1
In the recursion function, it is important to maintain the base value. If the base value is never invoked, in the above case, the infinite recursion function happened or it goes down to negative numbers. In the above example, countdown() function calls itself.
Solution
Recursion function is a function which is call itself. In the given example, we have called the recursion function to print the prime numbers from 10 to 1. A recursion function continues until some condition is met to prevent it. That's why, we use the if statement to break the infinite recursion.
Output of the above code
Number is 10 Number is 9 Number is 8 Number is 7 Number is 6 Number is 5 Number is 4 Number is 3 Number is 2 Number is 1In the recursion function, it is important to maintain the base value. If the base value is never invoked, in the above case, the infinite recursion function happened or it goes down to negative numbers. In the above example, countdown() function calls itself.
need an explanation for this answer? contact us directly to get an explanation for this answer