Recursions - Computer Science



Recursions

1. What is recursion?

a. Recursion in computer science is a way of thinking about and solving problems. In fact, recursion is one of the central ideas of computer science. Solving a problem using recursion means the solution depends on solutions to smaller instances of the same problem.

b. C++ programming language supports recursion by allowing one to define a call to a function within the definition of the function itself.

c. Recursion is often considered the opposite to Iteration; Iteration is where the solution to a problem can be constructed by applying the same method a number of times. (e.g. using a while/for loop)

2. A simple example: get sum of integers from 1 to n.

a. Using loop (Iteration solution):

int GetSum(int n)

{

int sum=0;

for (int i=1; i=0; i--)

std::cout ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download