Putting Recursion to Work: Calculating n Factorial

Our previous recursive methods have not presented us with any useful abilities. Before we look at examples of how we can exploit the mechanisms demonstrated so far to solve computational problems, let's line up the main ideas of how recursion can help us solve a problem.

Recursion is suited to solve problems that can be solved by repeatedly dividing a task into two smaller (or simpler) but similar problems. When the task has been divided a number of times (through recursive calls), a base case (see earlier) task will present itself and break the chain of recursive calls. The recursive method only knows the direct answer to the base case, but by combining the partial answers to the subtasks (held ...

Get C# Primer Plus now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.