0% found this document useful (0 votes)
27 views25 pages

Week 12 - Lecture 32 - Recursion

Uploaded by

rajabfrq
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views25 pages

Week 12 - Lecture 32 - Recursion

Uploaded by

rajabfrq
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Week# 12

Functions and Recursion

Dr Taimur Ahmed
Department of IT & CS, PAF-IAST
Lecture# 32
Recursion
Introduction to Recursion
❑ A recursive function is one that calls itself.

void Message(void)
{
cout << "This is a recursive function.\n";
Message();
}
❑ The above function displays the string
"This is a recursive function.\n", and then calls itself

Can you see a problem with this function?

Lecture# 32 - Recursive Functions | 3


Introduction to Recursion
❑ The function (on the last slide) is like an infinite loop because
there is no code to stop it from repeating.

❑ Like a loop, a recursive function must have some algorithm to


control the number of times it repeats.

Lecture# 32 - Recursive Functions | 4


Recursion with a Control Condition
void Message(int times)
{
if (times > 0)
{
cout << "This is a recursive function.\n";
Message(times - 1);
}
return;
}

❑ The function contains an if/else statement that controls the repetition. For
example, if we call the function:

Message(5);

The argument, 5, will cause the function to call itself 5 times.

Lecture# 32 - Recursive Functions | 5


Recursion with a Control Condition
void Message(int times)
{
if (times > 0) //Base case
{
cout << "This is a recursive function.\n";
Message(times - 1);
}
return;
}

❑ With each recursive call, the parameter controlling the recursion should move
closer to the base case

❑ Eventually, the parameter reaches the base case and the chain of recursive calls
terminates

Lecture# 32 - Recursive Functions | 6


Recursion with a Control Condition
1st call of the function void Message(int times)
{
Value of times: 5
if (times > 0)
{
2nd call of the function cout << "This is a recursive function.\n";
Value of times: 4 Message(times - 1);
}
3rd call of the function return;
}
Value of times: 3

4th call of the function

Value of times: 2

5th call of the function


This cycle repeats itself
until 0 is passed to the Value of times: 1
function.
6th call of the function

Depth of recursion: 6 Value of times: 0

Lecture# 32 - Recursive Functions | 7


Recursion with a Control Condition

Program Output

This is a recursive function.


This is a recursive function.
This is a recursive function.
This is a recursive function.
This is a recursive function.

Lecture# 32 - Recursive Functions | 8


Execution of a Recursive Function
Recursive Function – Execution
int main(){
int n=3;
fun(3) fun(3)
cout << "Function gives " << fun(n);
}

return 1 + fun(2) return 3


int fun(int n)
{
if(n==1) // base case return 1 + fun(1) return 2
return 1;
else
return 1 + fun(n-1); return 1 return 1
}

Function gives 3

Lecture# 32 - Recursive Functions | 10


What Happens When Called?
❑ Like the normal function execution, each time a recursive function is
called, a new copy of the function runs, with new instances of
parameters and local variables being created

❑ As each copy finishes executing, it returns to the copy of the


function that called it

❑ When the initial copy finishes executing, it returns to the part of


the program that made the initial call to the function

Lecture# 32 - Recursive Functions | 11


Recursive Function with two arguments – Execution
//sum of all integers between two integers
int main(){
sum(2,4) sum(m,n)
int m=2, n=4;
cout << "Sum = "<< sum(m, n);
return 0;
return 2 + fun(3,4) return 2+7
}

int sum(int m, int n){ return 3 + fun(4,4) return 3+4


if(m == n) //base case
return m;
else return 4 return 4
return m + sum(m+1, n);
}

Sum = 9

Lecture# 32 - Recursive Functions | 12


Types of Recursion
❑ Direct recursion
➢ a function calls itself

❑ Indirect recursion
➢ function A calls function B, and function B calls function A. Or,

➢ function A calls function B, which calls …, which calls function A

Lecture# 32 - Recursive Functions | 13


Recursive Function

Lecture# 32 - Recursive Functions | 14


Recursion
❑ To build all recursive functions:
1. Define the base case(s)
2. Define the recursive case(s)
a) Divide the problem into smaller sub-problems
b) Solve the sub-problems
c) Combine results to get answer

Lecture# 32 - Recursive Functions | 15


Recursion – Creating a Sum Function
❑ Consider creating a function which adds all number between a
number and 1
sum(5) = 5 + 4 + 3 + 2 + 1 = 15

Lecture# 32 - Recursive Functions | 16


Creating a Sum Function – Iterative
//Our initial total is zero
int total = 0;

//We want the sum from 1 + 2 + ... + 9 + 10


int n = 10;

/* The following for loop will calculate the


summation from 1 – n */
for ( int i = 1; i <= n; i++ ) {
total = total + i;
}
Lecture# 32 - Recursive Functions | 17
Creating a Sum Function – Recursive
int sum(int n) {

//Return 0 when n is 0
if ( n <= 0 )
return 0;
else //recursive call
return n + sum(n-1);

Lecture# 32 - Recursive Functions | 18


Creating a Factorial Function
❑ The factorial of a non-negative integer n is the product of all
positive integers less or equal to n

❑ Factorial of n is denoted by n!
❑ The factorial of 0 is = 1
0!=1
1!=1
2!=2x1
3!=3x2x1
4!=4x3x2x1

n ! = n x (n-1) x … x 2 x 1 if n > 0
Lecture# 32 - Recursive Functions | 19
Creating a Factorial Function – Iterative
0!=1
1!=1=1x0!
2!=2x1=2x1!
3!=3x2x1=3x2!
4!=4x3x2x1=4x3!

int fact = 1;
for (int j = 1; j <= n; j++)
{
fact = fact * j;
}
Lecture# 32 - Recursive Functions | 20
Creating a Factorial Function – Recursive
❑ Factorial of n can be expressed in terms of the factorial of n-1
Let’s define a function fact()

0!=1
1 ! = 1 = 1 x fact(0)
2 ! = 2 x 1 = 2 x fact(1)
3 ! = 3 x 2 x 1 = 3 x fact(2)
4 ! = 4 x 3 x 2 x 1 = 4 x fact(3)
n ! = n x (n-1) x (n-2) x … x 1 = n x fact(n-1)

Lecture# 32 - Recursive Functions | 21


Creating a Factorial Function – Recursive
0 ! = 1 // base case
1 ! = 1 = 1 x fact(0)
2 ! = 2 x 1 = 2 x fact(1)
3 ! = 3 x 2 x 1 = 3 x fact(2)
4 ! = 4 x 3 x 2 x 1 = 4 x fact(3)
n ! = n x (n-1) x (n-2) x … x 1 = n x fact(n-1)

int fact(int n)
{
if (n == 0) //Base case
return 1;
else
return n * fact(n-1);
}

Lecture# 32 - Recursive Functions | 22


Solving Recursively Defined Problems
❑ The natural definition of some problems leads to a recursive
solution

❑ Example: Fibonacci numbers:


1, 1, 2, 3, 5, 8, 13, 21, ...

❑ After the starting two numbers, each term is the sum of the two
preceding terms

❑ Recursive solution:
fib(n) = fib(n – 1) + fib(n – 2);

❑ Base cases: n == 0, n == 1
Lecture# 32 - Recursive Functions | 23
Solving Recursively Defined Problems

Lecture# 32 - Recursive Functions | 24


Recursion vs Iteration
❑ Recursive Implementation
+ Natural formulation of solution to certain problems
+ Results in shorter, simpler functions
➢ May not execute very efficiently

❑ Iterative Implementation
+ Executes more efficiently than recursion
➢ May not be as natural as recursion for some problems

Lecture# 32 - Recursive Functions | 25

You might also like