SlideShare uma empresa Scribd logo
1 de 12
Baixar para ler offline
Recursion
Intro, Recursive functions
Recursion vs Iteration
-> By Apni Kaksha <-
Recursion
A function can call other functions.
But a function calling itself seems logically wrong.
Recursion refers to a programming technique in which a function calls itself
directly or indirectly.
Direct/Indirect Recursions
def A( ):
A( )
def A( ):
B( )
def B( ):
A( )
Direct
Calling Itself
Indirect
Calling Itself
Recursive Function
def func1( ) :
print( ‘Hello World’ )
func1( )
This is a recursive function calling itself.
Recursion is Infinite...
To make it usable we need to make it finite.
We will make a case which will break the recursion.
def a(i):
if(i>0):
print(i)
i=i-1
a(i)
else:
return
Recursive Condition
Exit Condition
Base Case
We make a case whose result is already known. We use this case as the exit
case.
e.g. recursive code for factorial.
factorial(1) = 1 We know it already.
BASE CASE
Function to compute : an
Iterative definition : an
= a * a * a . . . a
n
Recursive definition : an
= a * an-1
Further an-1
= a * an-2
Recursive Definition
Practice Time
Q1. Find the output:
def binod(n):
if n==0:
print(‘Finally’)
else:
print(n)
binod(n-3)
binod(15)
Q2. Find the output:
def val(n):
if(n<=1):
return True
elif(n%2==0):
return val(n/2)
else:
return val(n/1)
Recursion & Loops are related, one thing can be done by both methods.
Sometimes Recursion is better, sometimes loop is better.
Recursion vs Iteration
When a loop runs, it repeats the same variables
and the same unit of code.
In Recursion for each recursive call, new memory
is allocated for the function.
● Recursion makes a program look shorter and easily
understandable in terms of mathematics also.
● Recursion is heavy on memory due to allocation of new memory
with each recursive call.
● When the code length & complexity matters, Recursion is
recommended.
● When the time & efficiency of the code matters, Iteration is
recommended.
Important Points
Practice Time
Q1. Write a program to print the string backwards (by both methods).
Do the following questions recursively.
Q2. Write a program to calculate ab
.
Q3. Write a program to calculate HCF.
Q4. Write a program to print fibonacci series.
14. Recursion.pdf

Mais conteúdo relacionado

Semelhante a 14. Recursion.pdf

35000120060_Nitesh Modi_CSE Presentation on recursion.pptx
35000120060_Nitesh Modi_CSE Presentation on recursion.pptx35000120060_Nitesh Modi_CSE Presentation on recursion.pptx
35000120060_Nitesh Modi_CSE Presentation on recursion.pptx
15AnasKhan
 
Python Programming unit5 (1).pdf
Python Programming unit5 (1).pdfPython Programming unit5 (1).pdf
Python Programming unit5 (1).pdf
jamvantsolanki
 
Concept_of_NAN_IND_INF_DEN_Using_C++
Concept_of_NAN_IND_INF_DEN_Using_C++Concept_of_NAN_IND_INF_DEN_Using_C++
Concept_of_NAN_IND_INF_DEN_Using_C++
Mohammed Nisamudheen
 

Semelhante a 14. Recursion.pdf (20)

lecture4-recursion.pptx
lecture4-recursion.pptxlecture4-recursion.pptx
lecture4-recursion.pptx
 
Recursion
RecursionRecursion
Recursion
 
35000120060_Nitesh Modi_CSE Presentation on recursion.pptx
35000120060_Nitesh Modi_CSE Presentation on recursion.pptx35000120060_Nitesh Modi_CSE Presentation on recursion.pptx
35000120060_Nitesh Modi_CSE Presentation on recursion.pptx
 
Recursion.pdf
Recursion.pdfRecursion.pdf
Recursion.pdf
 
Python Programming unit5 (1).pdf
Python Programming unit5 (1).pdfPython Programming unit5 (1).pdf
Python Programming unit5 (1).pdf
 
Recursion and looping
Recursion and loopingRecursion and looping
Recursion and looping
 
Concept_of_NAN_IND_INF_DEN_Using_C++
Concept_of_NAN_IND_INF_DEN_Using_C++Concept_of_NAN_IND_INF_DEN_Using_C++
Concept_of_NAN_IND_INF_DEN_Using_C++
 
Recursion.ppt
 Recursion.ppt Recursion.ppt
Recursion.ppt
 
lecture 10 Recursive Function and Macros.ppt
lecture 10 Recursive Function and Macros.pptlecture 10 Recursive Function and Macros.ppt
lecture 10 Recursive Function and Macros.ppt
 
Introduction to python programming ( part-2 )
Introduction to python programming ( part-2 )Introduction to python programming ( part-2 )
Introduction to python programming ( part-2 )
 
Data Structures- Part5 recursion
Data Structures- Part5 recursionData Structures- Part5 recursion
Data Structures- Part5 recursion
 
17recursion
17recursion17recursion
17recursion
 
Recursion | C++ | DSA
Recursion | C++ | DSARecursion | C++ | DSA
Recursion | C++ | DSA
 
Lecture 12 - Recursion
Lecture 12 - Recursion Lecture 12 - Recursion
Lecture 12 - Recursion
 
Lecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptxLecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptx
 
[ITP - Lecture 14] Recursion
[ITP - Lecture 14] Recursion[ITP - Lecture 14] Recursion
[ITP - Lecture 14] Recursion
 
Functional programming
Functional programmingFunctional programming
Functional programming
 
Algorithm and c language
Algorithm and c languageAlgorithm and c language
Algorithm and c language
 
Unit-I Recursion.pptx
Unit-I Recursion.pptxUnit-I Recursion.pptx
Unit-I Recursion.pptx
 
While loop
While loopWhile loop
While loop
 

Último

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Último (20)

Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptx
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 

14. Recursion.pdf

  • 1. Recursion Intro, Recursive functions Recursion vs Iteration -> By Apni Kaksha <-
  • 2. Recursion A function can call other functions. But a function calling itself seems logically wrong. Recursion refers to a programming technique in which a function calls itself directly or indirectly.
  • 3. Direct/Indirect Recursions def A( ): A( ) def A( ): B( ) def B( ): A( ) Direct Calling Itself Indirect Calling Itself
  • 4. Recursive Function def func1( ) : print( ‘Hello World’ ) func1( ) This is a recursive function calling itself.
  • 5. Recursion is Infinite... To make it usable we need to make it finite. We will make a case which will break the recursion. def a(i): if(i>0): print(i) i=i-1 a(i) else: return Recursive Condition Exit Condition
  • 6. Base Case We make a case whose result is already known. We use this case as the exit case. e.g. recursive code for factorial. factorial(1) = 1 We know it already. BASE CASE
  • 7. Function to compute : an Iterative definition : an = a * a * a . . . a n Recursive definition : an = a * an-1 Further an-1 = a * an-2 Recursive Definition
  • 8. Practice Time Q1. Find the output: def binod(n): if n==0: print(‘Finally’) else: print(n) binod(n-3) binod(15) Q2. Find the output: def val(n): if(n<=1): return True elif(n%2==0): return val(n/2) else: return val(n/1)
  • 9. Recursion & Loops are related, one thing can be done by both methods. Sometimes Recursion is better, sometimes loop is better. Recursion vs Iteration When a loop runs, it repeats the same variables and the same unit of code. In Recursion for each recursive call, new memory is allocated for the function.
  • 10. ● Recursion makes a program look shorter and easily understandable in terms of mathematics also. ● Recursion is heavy on memory due to allocation of new memory with each recursive call. ● When the code length & complexity matters, Recursion is recommended. ● When the time & efficiency of the code matters, Iteration is recommended. Important Points
  • 11. Practice Time Q1. Write a program to print the string backwards (by both methods). Do the following questions recursively. Q2. Write a program to calculate ab . Q3. Write a program to calculate HCF. Q4. Write a program to print fibonacci series.