Week 11

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

Recursive Functions

Lecture Link:
https://www.youtube.com/watch?v=dTUuazeX2Mk&list=PLVEVLI2v6thVDz7Ux
UPnURUKaqWFK7Z7v&index=32
Recursion

 The process in which a function calls


itself is known as recursion and the
corresponding function is called the
recursive function. The popular
example to understand the recursion
is factorial function.
Recursive Functions
 Factorial is a Special function which can
call itself

x10 = x * x9
x9 = x * x8
x8 = x * x7
……

xn = x * xn-1
Recursive Functions: Factorial
n! = n * (n-1) * (n-2) …….. 3 * 2 * 1
5! = 5 * 4 * 3 * 2 * 1
4! = 4 * 3 * 2 * 1

5! = 5 * 4!

0! = 1
Recursive Functions: Factorial

Int factorial ( int n )


{
if (n == 1 )
return ( n ) ;
else
return ( n * factorial (n-1) ) ;
}
 The recursion continues until some
condition is met.
 To prevent infinite recursion, if...else
statement (or similar approach) can be
used where one branch makes the
recursive call and other doesn't.
Example:
#include <iostream>
using namespace std;
int factorial(int);
int main()
{
int n;
cout<<"Enter a number to find factorial: ";
cin >> n;
cout << "Factorial of " << n <<" = " << factorial(n);
return 0;
}
int factorial(int n)
{
if (n > 1)
{
return n*factorial(n-1);
}
else
{
return 1;
}
}
Fibonacci Series

 The Fibonacci series is a sequence where


the next term is the sum of pervious two
terms. The first two terms of the Fibonacci
sequence is 0 followed by 1.
 The Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8,
13, 21
Example:

#include<iostream>
using namespace std;
int fibo(int n);
int main()
{
int n,i=0;
cout<<"\nEnter How Many Terms You Want To Print Fibonacci Series ";
cin>>n;
cout<<"\nFibonnaci Series Is Given Below\n\n";

while(i<n)
{
cout<<" "<<fibo(i);
i++;
}
cout<<" \n\n";
return 0;
}
int fibo(int n)
{
if((n==1)||(n==0))
{
return(n);
}
else
{
return(fibo(n-1)+fibo(n-2));
}
}
Example
The Fibonacci Series
 Set of recursive calls to function
fibonacci
f( 3 )

return f( 2 ) + f( 1 )

return f( 1 ) + f( 0 ) return 1

return 1 return 0
Advantages and Disadvantages of
Recursion

Advantages of C++ Recursion


 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 C++ Recursion
 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.
Recursion vs. iteration

Roughly speaking, recursion and iteration perform the same kinds of tasks:

Solve a complicated task one piece at a time, and combine the results.

Emphasis of iteration:

keep repeating until a task is “done”e.g.,


loop counter reaches limit,
linked list reaches null pointer,

Emphasis of recursion:
Solve a large problem by breaking it up into smaller and smaller pieces until you
can solve it; combine
the results.e.g.,
recursive factorial function

You might also like