SlideShare ist ein Scribd-Unternehmen logo
1 von 12
Downloaden Sie, um offline zu lesen
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

Weitere ähnliche Inhalte

Ähnlich wie 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
 

Ähnlich wie 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
 

Kürzlich hochgeladen

An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdf
SanaAli374401
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 

Kürzlich hochgeladen (20)

An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdf
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 

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.