It was a dark and stormy night. The crew said to the captain,
"Captain, tell us a story!" The captain said to the crew:
"It was a dark and stormy night. The crew said to the captain,
"Captain, tell us a story!" The captain said to the crew:
"It was a dark and stormy night." ...
This story has a story teller that tells the same story and it goes on forever. The following code prints the story by defining a function called story. After printing out the beginning of the story, it calls itself to print the same story that the story teller says. However, this code never ends.
In practice, we need to specify a point to stop. Otherwise, our program will run forever and eventually run out of memory. One way is to set up a counter to control it. The following code will end when the story has been told for 5 times. We use a parameter count to remember how many times a story still needs to be told. Each time story( ) calls itself, count decreases by 1. When count reaches 0, story( ) ends without calling itself again.