Title: Recursive Functions in C
1 Recursive Functions in C
2What is Recursion?
- Recursive having the characteristic of coming up
again, or repeating. - Recursive functions call themselves with smaller
and smaller versions (general cases) until a
solution is found. - Recursive functions are used to reduce a complex
problem to a simpler-to-solve problem (base case)
3Types of Recursion
- Direct
- a function calls itself
- Indirect
- function A calls function B, and function B calls
function A - function A calls function B, which calls , which
calls function A
A
B
B
A
N
4Recursion VS. Iteration
- Recursion
- Benefit
- Solves problems in a simple and elegant way
- Disadvantage
- May not execute efficiently
- Iteration
- Benefit
- Executes more efficiently
- Disadvantage
- Harder to code or understand
5Cases of Recursive Functions
- Base case The case for which the solution can be
stated non-recursively. - General case The case for which the solution is
expressed in terms of a smaller version of
itself also known as recursive case. - Recursive function/algorithm a solution that is
expressed in terms of (a) smaller instances of
itself and (b) a base case.
6When to Stop?
- A recursive function must always include a test
to determine if another recursive call should be
made, or if the recursion should stop with this
call - A different value is passed to the function each
time it is called - Eventually, the parameter reaches the value in
the test, and the recursion stops
7A Simple Example
- How to calculate XN?
- One solution using loop
- Another solution using a recursive function
- XN X XN-1
- XN-1 X XN-2
- XN-2 X XN-3
-
- X2 X X1
- XN X X X X X X X (X X X X
X)
General cases
Base case
N times
N -1 times
8Implementation of Power(x,n)
- include ltiostreamgt
- using namespace std
- int Power(int, int)
- int main()
-
- int number // Number that is being raised to
power - int exponent // Power the number is being
raised to - cin gtgt number gtgt exponent
- cout ltlt Power(number, exponent)
- return 0
-
- int Power(int x, // Number that is being raised
to power - int n ) // Power the number is being raised to
- if (n 1)
- return x
- else
- return x Power(x, n-1)
//Base case
//General cases
9Trace of the Execution
Power(2,3)
Return 8
x n 2 3
Call 1
Return 4
x n 2 2
Call 2
Return 2
x n 2 1
Call 3
10No Base Cases?
- What happens if there are no base cases?
- We have an infinite recursion, the recursive
equivalent of an infinite loop. - If we omit the condition if ( n1 )
- If we call Power with n less than or equal to 0.
11Recursive Calls Can not Go On Forever
- Each time a function is called, the computer
system creates temporary storage for the
parameters and the functions local variables.
Its called run-time stack. - When a function returns, its parameters and local
variables are released from the run-time stack. - Recursive function calls never return for
infinite recursion. More and more temporary
storage is used to store the new copies.
Eventually, all the memory space on the stack is
used. - STACK OVERFLOW. Program crashes.
12Stack Overflow
Stack Overflow
Call n1
Call n
Call 4
Call 3
Call 2
Call 1
Run-time Stack
13The Recursive Factorial Function
- The factorial function
- n! n(n-1)(n-2)...321 if n gt 0
- n! 1 if n 0
- Express factorial function as
- n! n (n-1)!
- base case n 0
14The Recursive Factorial Function
int Factorial (int num) if (num gt 0) return
num Factorial(num -1) else return 1
//Recursive call
//Base cases
15Fibonacci Numbers
- Fibonacci numbers
- 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...
- each number is the sum of the preceding two.
- Recursive definition
- Fib(0) 0 //Base case 1
- Fib(1) 1 //Base case 2
- Fib(n) Fib(n-1) Fib(n-2) //Recursive call
16Fibonacci Number Function
- int Fib (int n)
-
- if (n lt 0) //Base case 1
- return 0
- else if (n 1) //Base case 2
- return 1
- else
- return Fib(n 1) Fib(n 2) //Recursive
call
17Recursive Functions
- A function which calls itself with smaller
version - It has base cases and general cases
- Understand the problem
- Determine the base cases
- Determine the recursive cases
18Recursive Functions
- Hanoi tower a classic recursive problem
- consists of three rods, and a number of disks of
different sizes which can slide onto any rod. The
puzzle starts with the disks neatly stacked in
order of size on one rod, the smallest at the
top, thus making a conical shape. - The objective of the puzzle is to move the entire
stack to another rod, obeying the following
rules - Only one disk may be moved at a time.
- Each move consists of taking the upper disk from
one of the pegs and sliding it onto another rod,
on top of the other disks that may already be
present on that rod. - No disk may be placed on top of a smaller disk.
- http//en.wikipedia.org/wiki/Tower_of_Hanoi
19procedure Hanoi(n integer from, dest, by
char) Begin if (n1) then
writeln('Move the plate from ', from, ' to ',
dest) else begin Hanoi(n-1, from, by,
dest) Hanoi(1, from, dest, by)
Hanoi(n-1, by, dest, from) end End