SlideShare a Scribd company logo
4
Most read
5
Most read
6
Most read
Recursion
Disclaimer: This presentation is prepared by trainees of
baabtra as a part of mentoring program. This is not official
document of baabtra –Mentoring Partner
Baabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt .
Ltd
Recursion
Subhin P.V
111subru@gmail.com
www.facebook.com/subhinv
elayudhan
twitter.com/111subru
in.linkedin.com/in/Subhin P V
8129076036
WHAT IS RECURSION
• In simple language Recursion is the repeated
occurrence of a particular event
• In programming Recursion is the repeated
occurrence of a procedure as a result of the
procedure calling itself.
• Recursive procedure is the name given to the
procedures calling themselves.
• The parameters passed each time into the
function must be different.
Content of a Recursive Method
• Base case(s).
– Values of the input variables for which we perform no
recursive calls are called base cases (there should be at
least one base case).
– Every possible chain of recursive calls must eventually
reach a base case.
• Recursive calls.
– Calls to the current method.
– Each recursive call should be defined so that it makes
progress towards a base case.
Types of Recursion
• Single and Multiple Recursion
– Recursion that only contains a single self-
reference is known as single recursion. Eg:
Factorial function
– Recursion that contains multiple self-references is
known as multiple recursion. Eg: Fibonacci
function
Types of Recursion
• Direct And Indirect Recursion
– IF a Function F calls itself. (i.e) F calls F. Then this
recursion is Direct Recursion.
static int Direct (int n)
{
if (n<=0) return 0;
else return n + Direct (n-1);
}
Types of Recursion
• Indirect Recursion
– IF a Function F calls G. and Function G calls F. Then
this recursion is Indirect Recursion.
static int InDirect (int n)
{
if (n<=0) return 0;
else return n + Buddy (n-1);
}
int Buddy (int n)
{
return InDirect (n);
}
Recursively Defined Sequences
•Example:
•The sequence {an} of powers of 2 is given non
recursively by
an = 2n for n = 0, 1, 2, … .
•The same sequence can also be defined
Recursively:
a0 = 1
an+1 = 2an for n = 0, 1, 2, …
RECURSIVELY DEFINED FUNCTIONS
• Here is the non-recursive definition of the factorial
function:
– n! = 1· 2· 3· ··· · (n-1)· n
• Here is the recursive definition of a factorial:
(here f(n) = n!)
• The Java code for the Factorial function:
// recursive procedure for computing factorial
public static int Factorial(int n) {
if (n == 0) return 1; // base case
else return n * Factorial(n- 1); // recursive case
}
elsenfn
n
nf
)1(
0if1
)(
RECURSIVE ALGORITHMS
• Example: Recursive Fibonacci Algorithm
procedure fib(n: nonnegative integer)
if n = 0 then fib(0) := 0
else if n = 1 then fib(1) := 1
else fib(n) := fib(n – 1) + fib(n – 2)
RECURSIVE ALGORITHMS(contd..)
Recursive Fibonacci Evaluation:
Recursion
If this presentation helped you, please visit our
page facebook.com/baabtra and like it.
Thanks in advance.
www.baabtra.com | www.massbaab.com |www.baabte.com
Contact Us

More Related Content

PPTX
Computer worms viruses and Prevention
PPT
Data Transformation.ppt
PPTX
Introduction to iOS Apps Development
PPTX
Graph theory
PPT
Data Structures- Part5 recursion
PPTX
Responsive web designing ppt(1)
PPTX
introduction to ms office –word,excel & powerpoint
Computer worms viruses and Prevention
Data Transformation.ppt
Introduction to iOS Apps Development
Graph theory
Data Structures- Part5 recursion
Responsive web designing ppt(1)
introduction to ms office –word,excel & powerpoint

What's hot (20)

PPTX
Recursion
PPTX
6-Python-Recursion PPT.pptx
PPTX
Recursion
PPT
Function overloading(c++)
PPTX
Recursive Function
PPTX
Unary operator overloading
PPTX
Python Functions
PPTX
Array in c++
PPTX
Sorting Algorithms
PPT
Queue data structure
PPTX
Tuple in python
PPT
RECURSION IN C
PPTX
Functions in C
PDF
PPTX
Arrays In C++
PPTX
Doubly Linked List
PPTX
[OOP - Lec 19] Static Member Functions
PPTX
Queue in Data Structure
PPTX
Nested loops
PPTX
Inheritance in java
Recursion
6-Python-Recursion PPT.pptx
Recursion
Function overloading(c++)
Recursive Function
Unary operator overloading
Python Functions
Array in c++
Sorting Algorithms
Queue data structure
Tuple in python
RECURSION IN C
Functions in C
Arrays In C++
Doubly Linked List
[OOP - Lec 19] Static Member Functions
Queue in Data Structure
Nested loops
Inheritance in java
Ad

Similar to Recursion (17)

PDF
Python recursion
PPTX
Functions in python programming and its calling statement
PPTX
3-Recursive Function in programming .pptx
PPTX
Lecture02_Updated_Shallow Neural Networks.pptx
PPTX
Python recursion
PPTX
FUNCTIONS IN R PROGRAMMING.pptx
PPTX
Funtions of c programming. the functions of c helps to clarify all the tops
PPTX
recursion, syntax, types, example program
PDF
chapter-2-functionseng-1 (1).pdf
PPTX
Recursive Algorithm Detailed Explanation
PPTX
Lecture_7_StackAndRecursion (1).pptx
PDF
Lecture 2.4 Recursion.pdf
PDF
ESINF01-Recursion.pdfESINF01-Recursion.pdf
PPTX
Recursion
PDF
Python Programming unit5 (1).pdf
PDF
14. Recursion.pdf
PPTX
Simple program recursion problem solving.pptx
Python recursion
Functions in python programming and its calling statement
3-Recursive Function in programming .pptx
Lecture02_Updated_Shallow Neural Networks.pptx
Python recursion
FUNCTIONS IN R PROGRAMMING.pptx
Funtions of c programming. the functions of c helps to clarify all the tops
recursion, syntax, types, example program
chapter-2-functionseng-1 (1).pdf
Recursive Algorithm Detailed Explanation
Lecture_7_StackAndRecursion (1).pptx
Lecture 2.4 Recursion.pdf
ESINF01-Recursion.pdfESINF01-Recursion.pdf
Recursion
Python Programming unit5 (1).pdf
14. Recursion.pdf
Simple program recursion problem solving.pptx
Ad

More from baabtra.com - No. 1 supplier of quality freshers (20)

PPTX
Agile methodology and scrum development
PDF
Acquiring new skills what you should know
PDF
Baabtra.com programming at school
PDF
99LMS for Enterprises - LMS that you will love
PPTX
Chapter 6 database normalisation
PPTX
Chapter 5 transactions and dcl statements
PPTX
Chapter 4 functions, views, indexing
PPTX
PPTX
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
PPTX
Chapter 1 introduction to sql server
PPTX
Chapter 1 introduction to sql server
Agile methodology and scrum development
Acquiring new skills what you should know
Baabtra.com programming at school
99LMS for Enterprises - LMS that you will love
Chapter 6 database normalisation
Chapter 5 transactions and dcl statements
Chapter 4 functions, views, indexing
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
Chapter 1 introduction to sql server
Chapter 1 introduction to sql server

Recently uploaded (20)

PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
Pre independence Education in Inndia.pdf
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
01-Introduction-to-Information-Management.pdf
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PPTX
PPH.pptx obstetrics and gynecology in nursing
PPTX
master seminar digital applications in india
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
Business Ethics Teaching Materials for college
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
STATICS OF THE RIGID BODIES Hibbelers.pdf
human mycosis Human fungal infections are called human mycosis..pptx
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Pre independence Education in Inndia.pdf
Week 4 Term 3 Study Techniques revisited.pptx
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
01-Introduction-to-Information-Management.pdf
O7-L3 Supply Chain Operations - ICLT Program
Pharmacology of Heart Failure /Pharmacotherapy of CHF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PPH.pptx obstetrics and gynecology in nursing
master seminar digital applications in india
Renaissance Architecture: A Journey from Faith to Humanism
Business Ethics Teaching Materials for college
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Microbial diseases, their pathogenesis and prophylaxis
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
3rd Neelam Sanjeevareddy Memorial Lecture.pdf

Recursion

  • 2. Disclaimer: This presentation is prepared by trainees of baabtra as a part of mentoring program. This is not official document of baabtra –Mentoring Partner Baabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt . Ltd
  • 4. WHAT IS RECURSION • In simple language Recursion is the repeated occurrence of a particular event • In programming Recursion is the repeated occurrence of a procedure as a result of the procedure calling itself. • Recursive procedure is the name given to the procedures calling themselves. • The parameters passed each time into the function must be different.
  • 5. Content of a Recursive Method • Base case(s). – Values of the input variables for which we perform no recursive calls are called base cases (there should be at least one base case). – Every possible chain of recursive calls must eventually reach a base case. • Recursive calls. – Calls to the current method. – Each recursive call should be defined so that it makes progress towards a base case.
  • 6. Types of Recursion • Single and Multiple Recursion – Recursion that only contains a single self- reference is known as single recursion. Eg: Factorial function – Recursion that contains multiple self-references is known as multiple recursion. Eg: Fibonacci function
  • 7. Types of Recursion • Direct And Indirect Recursion – IF a Function F calls itself. (i.e) F calls F. Then this recursion is Direct Recursion. static int Direct (int n) { if (n<=0) return 0; else return n + Direct (n-1); }
  • 8. Types of Recursion • Indirect Recursion – IF a Function F calls G. and Function G calls F. Then this recursion is Indirect Recursion. static int InDirect (int n) { if (n<=0) return 0; else return n + Buddy (n-1); } int Buddy (int n) { return InDirect (n); }
  • 9. Recursively Defined Sequences •Example: •The sequence {an} of powers of 2 is given non recursively by an = 2n for n = 0, 1, 2, … . •The same sequence can also be defined Recursively: a0 = 1 an+1 = 2an for n = 0, 1, 2, …
  • 10. RECURSIVELY DEFINED FUNCTIONS • Here is the non-recursive definition of the factorial function: – n! = 1· 2· 3· ··· · (n-1)· n • Here is the recursive definition of a factorial: (here f(n) = n!) • The Java code for the Factorial function: // recursive procedure for computing factorial public static int Factorial(int n) { if (n == 0) return 1; // base case else return n * Factorial(n- 1); // recursive case } elsenfn n nf )1( 0if1 )(
  • 11. RECURSIVE ALGORITHMS • Example: Recursive Fibonacci Algorithm procedure fib(n: nonnegative integer) if n = 0 then fib(0) := 0 else if n = 1 then fib(1) := 1 else fib(n) := fib(n – 1) + fib(n – 2)
  • 14. If this presentation helped you, please visit our page facebook.com/baabtra and like it. Thanks in advance. www.baabtra.com | www.massbaab.com |www.baabte.com