SlideShare a Scribd company logo
CHAPTER- 6
Computer Science: Class XII ( As per CBSE Board)
INTRODUCTION TO RECURSION
1. A recursive function is one that calls itself.
2. It is one of the most powerful tool in programming
language. It is a process where function calls itself again and
again.
3. Recursion basically divides the big problem into small
problems up to the point where it can be solved easily, for
example if we have to calculate factorial of a 5, we will divide
factorial of 5 as 5*factorial (4), then 4*factorial (3), then
3*factorial (2), then 2*factorial (1) and now factorial of 1 can
be easily solved without any calculation, now each pending
function will be executed in reverse order.
RECURSION HAVE TWO PARTS
1.Base case(s) (non-recursive)
Recursion ends when the number reduces to 1. This is called
the base condition.
Every recursive function must have a base condition that
stops the recursion or else the function calls itself infinitely.
2. Recursive case(s)
The recursive calls should moves in such a way that each
time it comes closer to the base criteria.
By “simpler”, we mean “smaller” or “shorter” or “closer to the
base case”.
12
IMPORTANT NOTES
By default, the maximum depth of recursion is 1000. If the limit is
crossed, it results in Recursion Error. Let's look at one such
condition.
def recursor():
recursor()
recursor()
Output
Traceback (most recent call last):
File "<string>", line 3, in <module>
File "<string>", line 2, in a
File "<string>", line 2, in a
File "<string>", line 2, in a
[Previous line repeated 996 more times]
RecursionError: maximum recursion depth exceeded
HOW RECURSION WORKS?
• In execution of a program, the computer
creates function stack (LAST IN FIRST OUT).
• This function stack is a stack of frames.
• At every call of function, new parameter
value gets pushed to the stack. The top of the
stack always contains parameter value of last
call.
• When base case evaluates to true, values
from stack needs to pop out one by one and
result is to be returned after calculation.
RECURSION EXAMPLE
• A recursive function is one that calls itself.
def factorial(x):
if x==1: # BASE CASE
return 1
else:
return x*factorial(x-1) #R RECURSIVE CASE
Example:
f=factorial(5)
print ("factorial of 5 is ",f)
Example: Factorial
• n! = n × (n-1) × (n-2) × … × 1
2! =
3! =
4! =
2 × 1
3 × 2 × 1
4 × 3 × 2 × 1
• alternatively:
0! = 1
n! = n × (n-1)!
So 4! = 4 × 3!
3! = 3 × 2!
(Base case)
(Recursive case)
2! = 2 × 1! 1! = 1 ×
0!
RECURSION CONCEPTUALLY
4! = 4(3!)
3! = 3(2!)
2! = 2(1!)
1! = 1 (0!)
Base case
make smaller instances
of the same problem
RECURSION CONEPTUALLY……
4! = 4(3!)
3! = 3(2!)
2! = 2(1!)
1! = 1 (0!) = 1(1) = 1
Compute the base case
make smaller instances
of the same problem
RECURSION CONCEPTUALLY…
4! = 4(3!)
3! = 3(2!)
2! = 2(1!) = 2
1! = 1 (0!) = 1(1) = 1
Compute the base case
make smaller instances
of the same problem
build up
the result
=6
24
Recursion makes our program:
1. Easier to write.
2. Readable – Code is easier to read and
understand.
3. Reduce the lines of code – It takes less
lines of code to solve a problem using
recursion.
ADVANTAGE OF RECURSION
1.Not all problems can be solved using recursion.
2. If you don’t define the base case then the code
would run indefinitely.
3. Debugging is difficult in recursive functions as
the function is calling itself in a loop and it is hard
to understand which call is causing the issue.
4. Memory overhead – Call to the recursive
function is not memory efficient.
DIS-ADVANTAGE OF RECURSION
THANK YOU

More Related Content

PPTX
6-Python-Recursion PPT.pptx
Venkateswara Babu Ravipati
 
PPTX
Recursion part 1
Keerty Smile
 
PPTX
Recursion vs. Iteration: Code Efficiency & Structure
cogaxor346
 
PPTX
lecture4-recursion.pptx
Lizhen Shi
 
PPTX
Recursion in Data Structure
khudabux1998
 
PPT
Data Structures- Part5 recursion
Abdullah Al-hazmy
 
PDF
Data Structure - Lecture 2 - Recursion Stack Queue.pdf
donotreply20
 
PPTX
Python recursion
ToniyaP1
 
6-Python-Recursion PPT.pptx
Venkateswara Babu Ravipati
 
Recursion part 1
Keerty Smile
 
Recursion vs. Iteration: Code Efficiency & Structure
cogaxor346
 
lecture4-recursion.pptx
Lizhen Shi
 
Recursion in Data Structure
khudabux1998
 
Data Structures- Part5 recursion
Abdullah Al-hazmy
 
Data Structure - Lecture 2 - Recursion Stack Queue.pdf
donotreply20
 
Python recursion
ToniyaP1
 

Similar to 6 Recursion Using Python 1.pptx6 Recursion Using Python 1.pptx (20)

PDF
DS & Algo 2 - Recursion
Mohammad Imam Hossain
 
PPT
Recursion.ppt
TalhaHussain58
 
PPTX
Recursion
Jesmin Akhter
 
PPTX
Unit-I Recursion.pptx
ajajkhan16
 
PPT
recursion.ppt
NISHASOMSCS113
 
PPTX
Algorithm in computer science
Riazul Islam
 
PDF
Fibonacci Function Gallery - Part 2 - One in a series
Philip Schwarz
 
PDF
Recursion.pdf
Flavia Tembo Kambale
 
PPTX
WT-Pravesh Sakhare.pptx
TuleshwarGupta1
 
PPTX
Recursion is used in programming languages to use a procedure multiple times ...
najiyanasrink
 
PPTX
Recursion-in-Python for class third.pptx
raju909783
 
PPT
Lecture9 recursion
Muhammad Zubair
 
PPT
Cis068 08
FALLEE31188
 
PPT
FUNDAMETAL ALG.ppt
Menaka Sivakumar
 
PPTX
2022-9-recursive-1.pptx
RayLee547290
 
PPTX
Recursive Algorithm Detailed Explanation
Prapti Bhattacharjee
 
PPTX
DSA. Data structure algorithm dRecursion.pptx
siddiqsid0006
 
PPTX
Chapter 02 functions -class xii
Praveen M Jigajinni
 
PPTX
Recursion and Sorting Algorithms
Afaq Mansoor Khan
 
PDF
Dynamic programming
Jay Nagar
 
DS & Algo 2 - Recursion
Mohammad Imam Hossain
 
Recursion.ppt
TalhaHussain58
 
Recursion
Jesmin Akhter
 
Unit-I Recursion.pptx
ajajkhan16
 
recursion.ppt
NISHASOMSCS113
 
Algorithm in computer science
Riazul Islam
 
Fibonacci Function Gallery - Part 2 - One in a series
Philip Schwarz
 
Recursion.pdf
Flavia Tembo Kambale
 
WT-Pravesh Sakhare.pptx
TuleshwarGupta1
 
Recursion is used in programming languages to use a procedure multiple times ...
najiyanasrink
 
Recursion-in-Python for class third.pptx
raju909783
 
Lecture9 recursion
Muhammad Zubair
 
Cis068 08
FALLEE31188
 
FUNDAMETAL ALG.ppt
Menaka Sivakumar
 
2022-9-recursive-1.pptx
RayLee547290
 
Recursive Algorithm Detailed Explanation
Prapti Bhattacharjee
 
DSA. Data structure algorithm dRecursion.pptx
siddiqsid0006
 
Chapter 02 functions -class xii
Praveen M Jigajinni
 
Recursion and Sorting Algorithms
Afaq Mansoor Khan
 
Dynamic programming
Jay Nagar
 
Ad

More from usha raj (20)

PPTX
sql12.pptxsql12.pptxsql12.pptxsql12.pptx
usha raj
 
PPTX
CSPPT_class12.pptxclass11_CS_control statements7
usha raj
 
PPTX
memory -team 2.pptxmemory -team 2.pptxmemory -team 2.pptxmemory -team 2.pptx
usha raj
 
PPTX
ComputerNEtWorksComplete.pptxComputerNEtWorksComplete.pptx
usha raj
 
PPTX
List_tuple_dictionary.pptxList_tuple_dictionary.pptx
usha raj
 
PPTX
PYTHON_DATABASE_CONNECTIVITY.pptxPYTHON_DATABASE
usha raj
 
PPTX
7-2-data-structures-ii-stacks-queues.pptx
usha raj
 
PPTX
2_3 Functions 5d.pptx2_3 Functions 5d.pptx
usha raj
 
PPTX
APJ ABDUL KALAM (final draft).pptx sampl
usha raj
 
PPTX
BITCOIN PRICE PREDICTION USING MACHINE LEARNING.pptx
usha raj
 
PPTX
control-structure.pptxcontrol-structure.pptx
usha raj
 
PPTX
Cyber crimes and cyber security.pptx filetx
usha raj
 
PPTX
ResultAnalysis_PT3_Sahodaya_24_25_Class1_to_12.pptx
usha raj
 
PPTX
ResultAnalysis_PreBoard_Sahodaya24-25.pptx
usha raj
 
PPTX
python db connection samples and program
usha raj
 
PPTX
health and education presentation demonstration
usha raj
 
PPTX
Knee Osteoarthritis Detection and its Severity.pptx
usha raj
 
DOCX
Supporting schedule-management-plan-template-with-instructions
usha raj
 
DOC
Lut ls p_pv1_0
usha raj
 
PDF
Banking botreport
usha raj
 
sql12.pptxsql12.pptxsql12.pptxsql12.pptx
usha raj
 
CSPPT_class12.pptxclass11_CS_control statements7
usha raj
 
memory -team 2.pptxmemory -team 2.pptxmemory -team 2.pptxmemory -team 2.pptx
usha raj
 
ComputerNEtWorksComplete.pptxComputerNEtWorksComplete.pptx
usha raj
 
List_tuple_dictionary.pptxList_tuple_dictionary.pptx
usha raj
 
PYTHON_DATABASE_CONNECTIVITY.pptxPYTHON_DATABASE
usha raj
 
7-2-data-structures-ii-stacks-queues.pptx
usha raj
 
2_3 Functions 5d.pptx2_3 Functions 5d.pptx
usha raj
 
APJ ABDUL KALAM (final draft).pptx sampl
usha raj
 
BITCOIN PRICE PREDICTION USING MACHINE LEARNING.pptx
usha raj
 
control-structure.pptxcontrol-structure.pptx
usha raj
 
Cyber crimes and cyber security.pptx filetx
usha raj
 
ResultAnalysis_PT3_Sahodaya_24_25_Class1_to_12.pptx
usha raj
 
ResultAnalysis_PreBoard_Sahodaya24-25.pptx
usha raj
 
python db connection samples and program
usha raj
 
health and education presentation demonstration
usha raj
 
Knee Osteoarthritis Detection and its Severity.pptx
usha raj
 
Supporting schedule-management-plan-template-with-instructions
usha raj
 
Lut ls p_pv1_0
usha raj
 
Banking botreport
usha raj
 
Ad

Recently uploaded (20)

PPTX
Python-Application-in-Drug-Design by R D Jawarkar.pptx
Rahul Jawarkar
 
PDF
UTS Health Student Promotional Representative_Position Description.pdf
Faculty of Health, University of Technology Sydney
 
PPTX
How to Close Subscription in Odoo 18 - Odoo Slides
Celine George
 
PPTX
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
PPTX
Odoo 18 Sales_ Managing Quotation Validity
Celine George
 
PDF
Sunset Boulevard Student Revision Booklet
jpinnuck
 
PPT
Python Programming Unit II Control Statements.ppt
CUO VEERANAN VEERANAN
 
PPTX
Software Engineering BSC DS UNIT 1 .pptx
Dr. Pallawi Bulakh
 
PPTX
Artificial-Intelligence-in-Drug-Discovery by R D Jawarkar.pptx
Rahul Jawarkar
 
PPTX
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
PPTX
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
 
PDF
The Picture of Dorian Gray summary and depiction
opaliyahemel
 
PPTX
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
PDF
Virat Kohli- the Pride of Indian cricket
kushpar147
 
PPTX
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
PPTX
Kanban Cards _ Mass Action in Odoo 18.2 - Odoo Slides
Celine George
 
DOCX
Action Plan_ARAL PROGRAM_ STAND ALONE SHS.docx
Levenmartlacuna1
 
PDF
2.Reshaping-Indias-Political-Map.ppt/pdf/8th class social science Exploring S...
Sandeep Swamy
 
PPTX
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
PPTX
CARE OF UNCONSCIOUS PATIENTS .pptx
AneetaSharma15
 
Python-Application-in-Drug-Design by R D Jawarkar.pptx
Rahul Jawarkar
 
UTS Health Student Promotional Representative_Position Description.pdf
Faculty of Health, University of Technology Sydney
 
How to Close Subscription in Odoo 18 - Odoo Slides
Celine George
 
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
Odoo 18 Sales_ Managing Quotation Validity
Celine George
 
Sunset Boulevard Student Revision Booklet
jpinnuck
 
Python Programming Unit II Control Statements.ppt
CUO VEERANAN VEERANAN
 
Software Engineering BSC DS UNIT 1 .pptx
Dr. Pallawi Bulakh
 
Artificial-Intelligence-in-Drug-Discovery by R D Jawarkar.pptx
Rahul Jawarkar
 
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
 
The Picture of Dorian Gray summary and depiction
opaliyahemel
 
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
Virat Kohli- the Pride of Indian cricket
kushpar147
 
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
Kanban Cards _ Mass Action in Odoo 18.2 - Odoo Slides
Celine George
 
Action Plan_ARAL PROGRAM_ STAND ALONE SHS.docx
Levenmartlacuna1
 
2.Reshaping-Indias-Political-Map.ppt/pdf/8th class social science Exploring S...
Sandeep Swamy
 
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
CARE OF UNCONSCIOUS PATIENTS .pptx
AneetaSharma15
 

6 Recursion Using Python 1.pptx6 Recursion Using Python 1.pptx

  • 1. CHAPTER- 6 Computer Science: Class XII ( As per CBSE Board)
  • 2. INTRODUCTION TO RECURSION 1. A recursive function is one that calls itself. 2. It is one of the most powerful tool in programming language. It is a process where function calls itself again and again. 3. Recursion basically divides the big problem into small problems up to the point where it can be solved easily, for example if we have to calculate factorial of a 5, we will divide factorial of 5 as 5*factorial (4), then 4*factorial (3), then 3*factorial (2), then 2*factorial (1) and now factorial of 1 can be easily solved without any calculation, now each pending function will be executed in reverse order.
  • 3. RECURSION HAVE TWO PARTS 1.Base case(s) (non-recursive) Recursion ends when the number reduces to 1. This is called the base condition. Every recursive function must have a base condition that stops the recursion or else the function calls itself infinitely. 2. Recursive case(s) The recursive calls should moves in such a way that each time it comes closer to the base criteria. By “simpler”, we mean “smaller” or “shorter” or “closer to the base case”.
  • 4. 12 IMPORTANT NOTES By default, the maximum depth of recursion is 1000. If the limit is crossed, it results in Recursion Error. Let's look at one such condition. def recursor(): recursor() recursor() Output Traceback (most recent call last): File "<string>", line 3, in <module> File "<string>", line 2, in a File "<string>", line 2, in a File "<string>", line 2, in a [Previous line repeated 996 more times] RecursionError: maximum recursion depth exceeded
  • 5. HOW RECURSION WORKS? • In execution of a program, the computer creates function stack (LAST IN FIRST OUT). • This function stack is a stack of frames. • At every call of function, new parameter value gets pushed to the stack. The top of the stack always contains parameter value of last call. • When base case evaluates to true, values from stack needs to pop out one by one and result is to be returned after calculation.
  • 6. RECURSION EXAMPLE • A recursive function is one that calls itself. def factorial(x): if x==1: # BASE CASE return 1 else: return x*factorial(x-1) #R RECURSIVE CASE Example: f=factorial(5) print ("factorial of 5 is ",f)
  • 7. Example: Factorial • n! = n × (n-1) × (n-2) × … × 1 2! = 3! = 4! = 2 × 1 3 × 2 × 1 4 × 3 × 2 × 1 • alternatively: 0! = 1 n! = n × (n-1)! So 4! = 4 × 3! 3! = 3 × 2! (Base case) (Recursive case) 2! = 2 × 1! 1! = 1 × 0!
  • 8. RECURSION CONCEPTUALLY 4! = 4(3!) 3! = 3(2!) 2! = 2(1!) 1! = 1 (0!) Base case make smaller instances of the same problem
  • 9. RECURSION CONEPTUALLY…… 4! = 4(3!) 3! = 3(2!) 2! = 2(1!) 1! = 1 (0!) = 1(1) = 1 Compute the base case make smaller instances of the same problem
  • 10. RECURSION CONCEPTUALLY… 4! = 4(3!) 3! = 3(2!) 2! = 2(1!) = 2 1! = 1 (0!) = 1(1) = 1 Compute the base case make smaller instances of the same problem build up the result =6 24
  • 11. Recursion makes our program: 1. Easier to write. 2. Readable – Code is easier to read and understand. 3. Reduce the lines of code – It takes less lines of code to solve a problem using recursion. ADVANTAGE OF RECURSION
  • 12. 1.Not all problems can be solved using recursion. 2. If you don’t define the base case then the code would run indefinitely. 3. Debugging is difficult in recursive functions as the function is calling itself in a loop and it is hard to understand which call is causing the issue. 4. Memory overhead – Call to the recursive function is not memory efficient. DIS-ADVANTAGE OF RECURSION