117Chapter 6Control and Functionscountdown($num_arg - 1); } } (Photo web hosting)

117Chapter 6Control and Functionscountdown($num_arg - 1); } } countdown(10); This produces the browser output: Counting down from 10Counting down from 9Counting down from 8Counting down from 7Counting down from 6Counting down from 5Counting down from 4Counting down from 3Counting down from 2Counting down from 1As with all recursive functions, it s important to be sure that the function has a base case(a nonrecursive branch) in addition to the recursive case, and that the base case is certain toeventually occur. If the base case is never invoked, the situation is much like a whileloopwhere the test is always true we will have an infinite loop of function calling. In the case ofthe preceding function, we know that the base case will happen, because every invocation ofthe recursive case reduces the countdown number, which must eventually become zero. Ofcourse, this assumes that the input is a positive integer rather than a negative number or adouble. Notice that our greater than zero test guards against infinite recursion even in these cases, whereas a not equal to zero test would not. Similarly, mutually recursive functions(functions that call each other) work without a hitch. For example, the following definitions plus function call: function countdown_first ($num_arg) { if ($num_arg > 0) { print( Counting down (first) from $num_arg
); countdown_second($num_arg - 1); } } function countdown_second ($num_arg) { if ($num_arg > 0) { print( Counting down (second) from $num_arg
); countdown_first($num_arg - 1); } } countdown_first(5); produce the browser output: Counting down (first) from 5Counting down (second) from 4Counting down (first) from 3Counting down (second) from 2Counting down (first) from 108
You need excellent and relaible webhost company to host your web applications? Then pay a visit to Inexpensive Web Hosting services.

Leave a Reply