You are on page 1of 3

RECURSSION

Computer Programming(2015-16)

2/22/2016

1. A recursive function is a function that calls itself, either directly, or indirectly (through another
function).
2. [Note:Although many compilers allow function main to call itself, Section 3.6.1, paragraph 3,and
Section 5.2.2, paragraph 9, of the C++ standard document indicate that main should not be called
within a program or recursively. Its sole purpose is to be the starting point for program execution.]
3. A recursive function knows how to solve only the simplest case(s), or so-called base case(s). If the
function is called with a base case , the function simply returns a result.
4. If the function is called with a more complex problem, the function typically divides the problem into
two conceptual piecesa piece that the function knows how to do and a piece that it does not know
how to do. To make recursion feasible, the latter piece must resemble the original problem, but be a
slightly simpler or slightly smaller version of it.
5. For recursion to terminate, the sequence of recursive calls must converge on the base case.

EXAMPLE 1:

1
Ms. Fauzia Yasir

RECURSSION

Computer Programming(2015-16)

Self Study:

2/22/2016

0! = 1
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! = 5040
8! = 40320
9! = 362880
10! = 3628800

Fig. 6.30: fig06_30.cpp Testing the recursive fibonacci function.


2
Ms. Fauzia Yasir

RECURSSION

Computer Programming(2015-16)

2/22/2016

Task:
6.18 (Exponentiation) Write a function integerPower(base, exponent) that returns the value of
baseexponent. For example, integerPower(3, 4) = 3 * 3 * 3 * 3. Assume that exponent is a positive,
nonzero integer and that base is an integer. Do not use any math library functions.
6.36 (Recursive Exponentiation) Write a recursive function power( base, exponent ) that, when
invoked, returns baseexponent.
For example, power( 3, 4 ) = 3 * 3 * 3 * 3. Assume that exponent is an integer greater than or equal to
1.
Hint: The recursion step would use the relationship baseexponent = base base exponent 1 and the
terminating condition occurs when exponent is equal to 1, because base1 = base

3
Ms. Fauzia Yasir

You might also like