Recursion: by Yogita Deshmukh Department of Training and Placement VNIT Nagpur
Recursion: by Yogita Deshmukh Department of Training and Placement VNIT Nagpur
1
Recursion
void recurse()
{
1. A function that ... .. ...
calls itself is recurse();
known as a ... .. ...
recursive }
function.
And, this int main()
technique is {
known as ... .. ...
recursion. recurse();
... .. ... 2
By Yogita Deshmukh Department
of Training and}Placement VNIT
Functions
.
5
Function Aspects
Function declaration A function must be declared globally in
a c program to tell the compiler about the function name,
function parameters, and return type.
1
Function declaration return_type function_name
(argument list);
2
Function call function_name (argument_list)
3
Function definition return_type function_name
(argument list)
{
function
By Yogita Deshmukh Department of Training and body;
Placement VNIT Nagpur
}
7
By Yogita Deshmukh Department of Training and
Placement VNIT Nagpur
8
How are function calls
implemented?
■ The following applies in general, with minor variations
that are implementation dependent
◻ The system maintains a stack in memory
■ Stack is a last-in first-out structure
Local
Activation Variables
record Return Value
STACK
Return Addr
n! = n * (n-1)!
ComputeFactorial 12
◻ Factorial:
fact(0) = 1
fact(n) = n * fact(n-1), if n > 0
◻ Fibonacci series
(1,1,2,3,5,8,13,21,….) fib (0) = 1
fib (1) = 1
fib (n) = fib (n-1) + fib (n-2), if n > 1
if (4 = = 1) return
(1); else return (4
* fact(3));
if (4 = = 1) return
(1); else return (4
* fact(3));
if (3 = = 1) return
(1); else return (3
* fact(2));
long int fact (int n)
{
if (n = = 1) return
(1); else return (n *
fact(n-1));
} By Yogita Deshmukh Department 20
if (4 = = 1) return
(1); else return (4
* fact(3));
if (3 = = 1) return
(1); else return (3
* fact(2));
long int fact (int n)
{ if (2 = = 1) return
if (n = = 1) return (1); else return (2
* fact(1));
(1); else return (n *
fact(n-1));
} By Yogita Deshmukh Department 21
if (4 = = 1) return
(1); else return (4
* fact(3));
if (3 = = 1) return
(1); else return (3
* fact(2));
long int fact (int n)
{ if (2 = = 1)if return
(1 = = 1)
if (n = = 1) return (1); else return
return (2(1);
* fact(1));
(1); else return (n *
fact(n-1));
} By Yogita Deshmukh Department 22
if (4 = = 1) return
(1); else return (4
* fact(3));
if (3 = = 1) return
(1); else return
if (3
(2 = = 1) return
* fact(2)); (1); else return (2 1
long int fact (int n) * fact(1));
{ if (1 = = 1)
if (n = = 1) return return (1);
(1); else return (n *
fact(n-1));
} By Yogita Deshmukh Department 23
if (4 = = 1) return
(1); else return (4
* fact(3));
if (3 = = 1) return
(1); else return (3 2
* fact(2));
if (2 = = 1) return
(1); else return (2 1
long int fact (int n) * fact(1));
{ if (1 = = 1)
if (n = = 1) return return (1);
(1); else return (n *
fact(n-1));
} By Yogita Deshmukh Department 24
if (4 = = 1) return
(1); else return (4
* fact(3));
if (3 = = 1) return
(1); else return (3 2
* fact(2));
if (2 = = 1) return
(1); else return (2 1
long int fact (int n) * fact(1));
{ if (1 = = 1)
if (n = = 1) return return (1);
(1); else return (n *
fact(n-1));
} By Yogita Deshmukh Department 25
Local
Activation Variables
record Return Value
STACK
Return Addr
3 times
Before call Call ncr Call fact fact returns ncr returns
Local
Variables
Return Value
Return Addr
n=0
1
RA .. fact fact
n=1 n=1 n=1 returns
main - - 1*1 = 1
calls to main
RA .. fact RA .. fact RA .. fact
fact n=2 n=2 n=2 n=2 n=2
- - - - 2*1 = 2
RA .. fact RA .. fact RA .. fact RA .. fact RA .. fact
n=3 n=3 n=3 n=3 n=3 n=3 n=3
- - - - - - 3*2 = 6
RA .. main RA .. main RA .. RA .. main RA .. main RA .. main RA .. main
main
void main() {
printf(“Fib(4) is: %d \n”, f(4));
main }
By Yogita Deshmukh Department 36
of Training and Placement VNIT
Fibonacci Numbers
Fibonacci recurrence:
fib(n) = 1 if n = 0 or 1;
= fib(n – 2) + fib(n – 1)
otherwise;
fib (0) fib (1) fib (0) fib (1) fib (1) fib (2)
fib (0) fib (1) fib (0) fib (1) fib (1) fib (2)
1 1 1 1 1
110 245
sumSquares(5,7) sumSquares(8,10)
61 49 145 100
sumSquares(5,6) sumSquares(7,7) sumSquares(8,9) sumSquares(10,10)
25 36 64 81
sumSquares(5,5) sumSquares(6,6) sumSquares(8,8) sumSquares(9,9)
25 36 49 64 Department 81 100 23
43
By Yogita Deshmukh
of Training and Placement VNIT
Towers of Hanoi Problem
1
2
3
4
5
fib (0) fib (1) fib (0) fib (1) fib (1) fib (2)