|
This program will not
continue forever, however. The computer keeps function calls on a stack and
once too many are called without ending, the program will terminate. Why not
write a program to see how many times the function is called before the
program terminates?
It can be thought of like those little Russian dolls that always have a
smaller doll inside. Each doll calls another doll, and you can think of the
size being a counter variable that is being decremented by one.
Think
of a really tiny doll, the size of a few atoms. You can't get any smaller
than that, so there are no more dolls. Normally, a recursive function will
have a variable that performs a similar action; one that controls when the
function will finally exit. The condition where the function will not call
itself is termed the base case of the function. Basically, it is an
if-statement that checks some variable for a condition (such as a number
being less than zero, or greater than some other number) and if that
condition is true, it will not allow the function to call itself again. (Or,
it could check if a certain condition is true and only then allow the
function to call itself)
|