Download as pdf or txt
Download as pdf or txt
You are on page 1of 24

AAST

Recursion
Recursion
In general, programmers use two approaches to
writing repetitive algorithms. One approach uses
loops; the other uses recursion. Recursion is a
repetitive process in which a function calls itself.

2
PROGRAM 1 Iterative Factorial Function

4
FORMULA Iterative Factorial Definition

FORMULA Recursive Factorial Definition

5
PROGRAM 1 Recursive Factorial Function

6
FIGURE Factorial (3) Recursively

7
Note
Every recursive call must either solve part of the problem
or reduce the size of the problem.

11
Example 1: Multiplication
• Suppose we wish to write a recursive function to multiply an
integer m by another integer n using addition.
[We can add, but we only know how to multiply by 1].
• The best way to go about this is to formulate the solution by
identifying the base case and the recursive case.
• The base case is if n is 1. The answer is m.
• The recursive case is: m*n = m + m (n-1). m, n = 1
m*n
m + m (n-1), n>1

Dr. Marwa Elshenawy


Recursive Function Multiply
int multiply ( int m, int n)
{
int ans;
if (n==1)
ans=m;
else
ans= m + multiply (m, n-1);
return ans;
}
Example 1: Multiplication …
Expansion
phase

multiply(6,3) = 6 + multiply(6, 2)
= 6 + (6 + multiply(6, 1))
= 6+ (6 + (6 )

= 6 + (6 + (6))
= 6 + (12) Substitution
= 18 phase

14
Recursive Function Multiply
multiply (6,3)

m is 6
n is 3
18
3 == 1 is false
ans is 6 + multiply (6,2)
return (ans)

m is 6
n is 2
12
2 == 1 is false
ans is 6 + multiply (6,1)
return (ans)

m is 6
n is 1
6 1 == 1 is true

return (6)
Example 3: Power function
• Suppose we wish to define our own power function that
raise a double number to the power of a non-negative
integer exponent. xn , n>=0.
• The base case is if n is 0. The answer is 1.
• The recursive case is: xn = x * xn-1.

1, n = 0
xn
x * x n-1, n>0

16
Example 3: Power function …

int pow(int x, int n)


{
if (n == 0)
return 1; /* simple case */
else
return x * pow(x, n - 1); /* recursive step */
}

17
Fibonacci Series
• The Fibonacci numbers are the numbers in the following integer
sequence.
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ……
PROGRAM 2 Recursive Fibonacci

19
FIGURE Fibonacci Numbers

20
Recursion vs. iteration
• Iteration can be used in place of recursion
• An iterative algorithm uses a looping construct
• A recursive algorithm uses a branching structure
• Recursive solutions are often less efficient, in
terms of both time and space, than iterative
solutions
• Recursion can simplify the solution of a
problem, often resulting in shorter, more easily
understood source code
How do I write a recursive function?
• Determine the size factor
• Determine the base case(s)
(the one for which you know the answer)
• Determine the general case(s)
(the one where the problem is expressed as a smaller version of itself)
• Verify the algorithm
(use the "Three-Question-Method")
Advantages of Recursive Function
● It makes our code shorter and cleaner.
● Recursion is required in problems concerning data structures and advanced
algorithms, such as Graph and Tree Traversal.
Disadvantages of Recursive Function

● It takes a lot of stack space compared to an iterative program.


● It uses more processor time.
● It can be more difficult to debug compared to an equivalent iterative program.

You might also like