SlideShare ist ein Scribd-Unternehmen logo
1 von 18
Chapter 15:
Recursion
Objectives
• In this chapter, you will:
– Learn about recursive definitions
– Explore the base case and the general case of a
recursive definition
– Discover what a recursive algorithm is
– Learn about recursive functions
2C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Objectives (cont’d.)
– Become familiar with direct and indirect recursion
– Explore how to use recursive functions to
implement recursive algorithms
– Become aware of recursion vs. iteration
3C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Recursive Definitions
• Recursion: solving a problem by reducing it to
smaller versions of itself
– Provides a powerful way to solve certain problems
which would be complicated otherwise
4C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Recursive Definitions (cont’d.)
• Recursive definition: defining a problem in
terms of a smaller version of itself
• Base case: the case for which the solution is
obtained directly
– Every recursive definition must have one (or
more) base case(s)
– The base case stops the recursion
• General case: must eventually reduce to a base
case
5C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Recursive Definitions (cont’d.)
• Example: factorials
0! = 1 (1)
n! = n x (n-1)! if n > 0 (2)
– Equation (1) is called the base case
– Equation (2) is called the general case
6C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Recursive Definitions (cont’d.)
• Recursive algorithm: finds a solution by
reducing problem to smaller versions of itself
– Must have one (or more) base cases
– General solution must eventually reduce to a base
case
• Recursive function: a function that calls itself
• Recursive algorithms are implemented using
recursive functions
7C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Recursive Definitions (cont’d.)
• Think of a recursive function as having
infinitely many copies of itself
– Every call has its own code and its own set of
parameters and local variables
– After completing a particular recursive call:
• Control goes back to the calling environment, the
previous call
• Execution begins from the point immediately following
the recursive call
8C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Direct and Indirect Recursion
• Directly recursive: a function that calls itself
• Indirectly recursive: a function that calls
another function and eventually results in the
original function call
• Tail recursive function: recursive function in
whose last statement executed is the
recursive call
9C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Infinite Recursion
• Infinite recursion: every recursive call results
in another recursive call
– In theory, infinite recursion executes forever
• Because computer memory is finite:
– Function executes until the system runs out of
memory
– Results in an abnormal program termination
10C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Infinite Recursion (cont’d.)
• To design a recursive function:
– Understand problem requirements
– Determine limiting conditions
– Identify base cases and provide a direct solution
to each base case
– Identify general cases and provide a solution to
each general case in terms of smaller versions of
itself
11C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Recursion or Iteration?
• Iterative control structure: uses a loop to
repeat a set of statements
• There are usually two ways to solve a
particular problem:
– Iteration (looping)
– Recursion
• When choosing, must consider:
– Nature of the problem
– Efficiency
12C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Recursion or Iteration? (cont’d.)
• Whenever a function is called
– Memory space for its formal parameters and
(automatic) local variables is allocated
• When the function terminates
– That memory space is then deallocated
• Every (recursive) call has its own set of
parameters and (automatic) local variables
13C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Recursion or Iteration? (cont’d.)
• Overhead associated with executing a
(recursive) function in terms of:
– Memory space
– Computer time
• A recursive function executes more slowly
than its iterative counterpart
• Today’s computers are fast
– Overhead of a recursion function is not noticeable
14C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Recursion or Iteration? (cont’d.)
• Sometimes iterative solution is more obvious
and easier to understand
• If the definition of a problem is inherently
recursive, consider a recursive solution
15C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Summary
• Recursion: process of solving a problem by
reducing it to smaller versions of itself
• Recursive definition: defines a problem in
terms of smaller versions of itself
– Has one or more base cases
• Recursive algorithm: solves a problem by
reducing it to smaller versions of itself
– Has one or more base cases
16C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Summary (cont’d.)
• The solution to the problem in a base case is
obtained directly
• Recursive function: function that calls itself
– Must have one or more base cases
• Recursive algorithms are implemented using
recursive functions
• The general solution breaks the problem into
smaller versions of itself
17C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Summary (cont’d.)
• The general case must eventually be reduced
to a base case
– The base case stops the recursion
• Directly recursive: a function calls itself
• Indirectly recursive: a function calls another
function and eventually calls the original
• Tail recursive: the last statement executed is
the recursive call
18C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Weitere ähnliche Inhalte

Was ist angesagt?

9781285852744 ppt ch02
9781285852744 ppt ch029781285852744 ppt ch02
9781285852744 ppt ch02Terry Yoast
 
9781285852744 ppt ch18
9781285852744 ppt ch189781285852744 ppt ch18
9781285852744 ppt ch18Terry Yoast
 
9781285852744 ppt ch08
9781285852744 ppt ch089781285852744 ppt ch08
9781285852744 ppt ch08Terry Yoast
 
9781285852744 ppt ch16
9781285852744 ppt ch169781285852744 ppt ch16
9781285852744 ppt ch16Terry Yoast
 
9781285852744 ppt ch14
9781285852744 ppt ch149781285852744 ppt ch14
9781285852744 ppt ch14Terry Yoast
 
Chapter 1 - An Overview of Computers and Programming Languages
Chapter 1 - An Overview of Computers and Programming LanguagesChapter 1 - An Overview of Computers and Programming Languages
Chapter 1 - An Overview of Computers and Programming LanguagesAdan Hubahib
 
Csc153 chapter 05
Csc153 chapter 05Csc153 chapter 05
Csc153 chapter 05PCC
 
9781111530532 ppt ch07
9781111530532 ppt ch079781111530532 ppt ch07
9781111530532 ppt ch07Terry Yoast
 
Chapter 13 - Recursion
Chapter 13 - RecursionChapter 13 - Recursion
Chapter 13 - RecursionAdan Hubahib
 
Csc153 chapter 02
Csc153 chapter 02Csc153 chapter 02
Csc153 chapter 02PCC
 
Can programming be liberated from the von neumann style?
Can programming be liberated from the von neumann style?Can programming be liberated from the von neumann style?
Can programming be liberated from the von neumann style?Oriol López Massaguer
 
Control Flow Graphs
Control Flow GraphsControl Flow Graphs
Control Flow Graphsdaimk2020
 
9 subprograms
9 subprograms9 subprograms
9 subprogramsjigeno
 
Basics of reflection
Basics of reflectionBasics of reflection
Basics of reflectionkim.mens
 

Was ist angesagt? (20)

9781285852744 ppt ch02
9781285852744 ppt ch029781285852744 ppt ch02
9781285852744 ppt ch02
 
9781285852744 ppt ch18
9781285852744 ppt ch189781285852744 ppt ch18
9781285852744 ppt ch18
 
9781285852744 ppt ch08
9781285852744 ppt ch089781285852744 ppt ch08
9781285852744 ppt ch08
 
9781285852744 ppt ch16
9781285852744 ppt ch169781285852744 ppt ch16
9781285852744 ppt ch16
 
9781285852744 ppt ch14
9781285852744 ppt ch149781285852744 ppt ch14
9781285852744 ppt ch14
 
Chapter 1 - An Overview of Computers and Programming Languages
Chapter 1 - An Overview of Computers and Programming LanguagesChapter 1 - An Overview of Computers and Programming Languages
Chapter 1 - An Overview of Computers and Programming Languages
 
Lesson 13 object and class
Lesson 13 object and classLesson 13 object and class
Lesson 13 object and class
 
Csc153 chapter 05
Csc153 chapter 05Csc153 chapter 05
Csc153 chapter 05
 
9781111530532 ppt ch07
9781111530532 ppt ch079781111530532 ppt ch07
9781111530532 ppt ch07
 
Chapter 13 - Recursion
Chapter 13 - RecursionChapter 13 - Recursion
Chapter 13 - Recursion
 
Csc153 chapter 02
Csc153 chapter 02Csc153 chapter 02
Csc153 chapter 02
 
10. sub program
10. sub program10. sub program
10. sub program
 
Can programming be liberated from the von neumann style?
Can programming be liberated from the von neumann style?Can programming be liberated from the von neumann style?
Can programming be liberated from the von neumann style?
 
08 subprograms
08 subprograms08 subprograms
08 subprograms
 
Yacc
YaccYacc
Yacc
 
Ladc presentation
Ladc presentationLadc presentation
Ladc presentation
 
Control Flow Graphs
Control Flow GraphsControl Flow Graphs
Control Flow Graphs
 
9 subprograms
9 subprograms9 subprograms
9 subprograms
 
Basics of reflection
Basics of reflectionBasics of reflection
Basics of reflection
 
Csci360 08-subprograms
Csci360 08-subprogramsCsci360 08-subprograms
Csci360 08-subprograms
 

Andere mochten auch

Andere mochten auch (15)

Software development
Software developmentSoftware development
Software development
 
9781285852744 ppt ch10
9781285852744 ppt ch109781285852744 ppt ch10
9781285852744 ppt ch10
 
Computer science education week or csed week
Computer science education week or csed weekComputer science education week or csed week
Computer science education week or csed week
 
Optbook
OptbookOptbook
Optbook
 
9781439035665 ppt ch04
9781439035665 ppt ch049781439035665 ppt ch04
9781439035665 ppt ch04
 
Savitch ch 05
Savitch ch 05Savitch ch 05
Savitch ch 05
 
Chapter 05
Chapter 05Chapter 05
Chapter 05
 
Algorithm
AlgorithmAlgorithm
Algorithm
 
9780840024220 ppt ch06
9780840024220 ppt ch069780840024220 ppt ch06
9780840024220 ppt ch06
 
JavaYDL14
JavaYDL14JavaYDL14
JavaYDL14
 
Computer basics
Computer basicsComputer basics
Computer basics
 
raid technology
raid technologyraid technology
raid technology
 
Computer operating systems
Computer operating systemsComputer operating systems
Computer operating systems
 
Lesson 1 introduction
Lesson 1 introductionLesson 1 introduction
Lesson 1 introduction
 
Lecture 2
Lecture 2Lecture 2
Lecture 2
 

Ähnlich wie 9781285852744 ppt ch15

CH-1.1 Introduction (1).pptx
CH-1.1 Introduction (1).pptxCH-1.1 Introduction (1).pptx
CH-1.1 Introduction (1).pptxsatvikkushwaha1
 
Introduction to Algorithms And DataStructure
Introduction to Algorithms And DataStructureIntroduction to Algorithms And DataStructure
Introduction to Algorithms And DataStructurePrasanna996462
 
9781423902096_PPT_ch07.ppt
9781423902096_PPT_ch07.ppt9781423902096_PPT_ch07.ppt
9781423902096_PPT_ch07.pptLokeshK66
 
9781111530532 ppt ch13
9781111530532 ppt ch139781111530532 ppt ch13
9781111530532 ppt ch13Terry Yoast
 
Intro To C++ - Class #20: Functions, Recursion
Intro To C++ - Class #20: Functions, RecursionIntro To C++ - Class #20: Functions, Recursion
Intro To C++ - Class #20: Functions, RecursionBlue Elephant Consulting
 
Dynamic programming class 16
Dynamic programming class 16Dynamic programming class 16
Dynamic programming class 16Kumar
 
QA CHAPTER II.pptx
QA CHAPTER II.pptxQA CHAPTER II.pptx
QA CHAPTER II.pptxTeshome48
 
Programs_Problem_Solving_Algorithms.ppt
Programs_Problem_Solving_Algorithms.pptPrograms_Problem_Solving_Algorithms.ppt
Programs_Problem_Solving_Algorithms.pptmalik681299
 
UNIT-2 Quantitaitive Anlaysis for Mgt Decisions.pptx
UNIT-2 Quantitaitive Anlaysis for Mgt Decisions.pptxUNIT-2 Quantitaitive Anlaysis for Mgt Decisions.pptx
UNIT-2 Quantitaitive Anlaysis for Mgt Decisions.pptxMinilikDerseh1
 
Review chapter 1 2-3
Review chapter 1 2-3Review chapter 1 2-3
Review chapter 1 2-3ahmed22dg
 

Ähnlich wie 9781285852744 ppt ch15 (20)

CH-1.1 Introduction (1).pptx
CH-1.1 Introduction (1).pptxCH-1.1 Introduction (1).pptx
CH-1.1 Introduction (1).pptx
 
Introduction to Algorithms And DataStructure
Introduction to Algorithms And DataStructureIntroduction to Algorithms And DataStructure
Introduction to Algorithms And DataStructure
 
9781423902096_PPT_ch07.ppt
9781423902096_PPT_ch07.ppt9781423902096_PPT_ch07.ppt
9781423902096_PPT_ch07.ppt
 
9781111530532 ppt ch13
9781111530532 ppt ch139781111530532 ppt ch13
9781111530532 ppt ch13
 
Chapter 13
Chapter 13Chapter 13
Chapter 13
 
LP.ppt
LP.pptLP.ppt
LP.ppt
 
RECURSION.pptx
RECURSION.pptxRECURSION.pptx
RECURSION.pptx
 
Python recursion
Python recursionPython recursion
Python recursion
 
Intro To C++ - Class #20: Functions, Recursion
Intro To C++ - Class #20: Functions, RecursionIntro To C++ - Class #20: Functions, Recursion
Intro To C++ - Class #20: Functions, Recursion
 
Dynamic programming class 16
Dynamic programming class 16Dynamic programming class 16
Dynamic programming class 16
 
Ch02
Ch02Ch02
Ch02
 
part_1 (1).ppt
part_1 (1).pptpart_1 (1).ppt
part_1 (1).ppt
 
SE-software design.ppt
SE-software design.pptSE-software design.ppt
SE-software design.ppt
 
QA CHAPTER II.pptx
QA CHAPTER II.pptxQA CHAPTER II.pptx
QA CHAPTER II.pptx
 
5 software design
5 software design5 software design
5 software design
 
Programs_Problem_Solving_Algorithms.ppt
Programs_Problem_Solving_Algorithms.pptPrograms_Problem_Solving_Algorithms.ppt
Programs_Problem_Solving_Algorithms.ppt
 
UNIT-2 Quantitaitive Anlaysis for Mgt Decisions.pptx
UNIT-2 Quantitaitive Anlaysis for Mgt Decisions.pptxUNIT-2 Quantitaitive Anlaysis for Mgt Decisions.pptx
UNIT-2 Quantitaitive Anlaysis for Mgt Decisions.pptx
 
Review chapter 1 2-3
Review chapter 1 2-3Review chapter 1 2-3
Review chapter 1 2-3
 
Dynamic pgmming
Dynamic pgmmingDynamic pgmming
Dynamic pgmming
 
Chapter 4 5
Chapter 4 5Chapter 4 5
Chapter 4 5
 

Mehr von Terry Yoast

9781305078444 ppt ch12
9781305078444 ppt ch129781305078444 ppt ch12
9781305078444 ppt ch12Terry Yoast
 
9781305078444 ppt ch11
9781305078444 ppt ch119781305078444 ppt ch11
9781305078444 ppt ch11Terry Yoast
 
9781305078444 ppt ch10
9781305078444 ppt ch109781305078444 ppt ch10
9781305078444 ppt ch10Terry Yoast
 
9781305078444 ppt ch09
9781305078444 ppt ch099781305078444 ppt ch09
9781305078444 ppt ch09Terry Yoast
 
9781305078444 ppt ch08
9781305078444 ppt ch089781305078444 ppt ch08
9781305078444 ppt ch08Terry Yoast
 
9781305078444 ppt ch07
9781305078444 ppt ch079781305078444 ppt ch07
9781305078444 ppt ch07Terry Yoast
 
9781305078444 ppt ch06
9781305078444 ppt ch069781305078444 ppt ch06
9781305078444 ppt ch06Terry Yoast
 
9781305078444 ppt ch05
9781305078444 ppt ch059781305078444 ppt ch05
9781305078444 ppt ch05Terry Yoast
 
9781305078444 ppt ch04
9781305078444 ppt ch049781305078444 ppt ch04
9781305078444 ppt ch04Terry Yoast
 
9781305078444 ppt ch03
9781305078444 ppt ch039781305078444 ppt ch03
9781305078444 ppt ch03Terry Yoast
 
9781305078444 ppt ch02
9781305078444 ppt ch029781305078444 ppt ch02
9781305078444 ppt ch02Terry Yoast
 
9781305078444 ppt ch01
9781305078444 ppt ch019781305078444 ppt ch01
9781305078444 ppt ch01Terry Yoast
 
9781337102087 ppt ch13
9781337102087 ppt ch139781337102087 ppt ch13
9781337102087 ppt ch13Terry Yoast
 
9781337102087 ppt ch18
9781337102087 ppt ch189781337102087 ppt ch18
9781337102087 ppt ch18Terry Yoast
 
9781337102087 ppt ch17
9781337102087 ppt ch179781337102087 ppt ch17
9781337102087 ppt ch17Terry Yoast
 
9781337102087 ppt ch16
9781337102087 ppt ch169781337102087 ppt ch16
9781337102087 ppt ch16Terry Yoast
 
9781337102087 ppt ch15
9781337102087 ppt ch159781337102087 ppt ch15
9781337102087 ppt ch15Terry Yoast
 
9781337102087 ppt ch14
9781337102087 ppt ch149781337102087 ppt ch14
9781337102087 ppt ch14Terry Yoast
 
9781337102087 ppt ch12
9781337102087 ppt ch129781337102087 ppt ch12
9781337102087 ppt ch12Terry Yoast
 
9781337102087 ppt ch11
9781337102087 ppt ch119781337102087 ppt ch11
9781337102087 ppt ch11Terry Yoast
 

Mehr von Terry Yoast (20)

9781305078444 ppt ch12
9781305078444 ppt ch129781305078444 ppt ch12
9781305078444 ppt ch12
 
9781305078444 ppt ch11
9781305078444 ppt ch119781305078444 ppt ch11
9781305078444 ppt ch11
 
9781305078444 ppt ch10
9781305078444 ppt ch109781305078444 ppt ch10
9781305078444 ppt ch10
 
9781305078444 ppt ch09
9781305078444 ppt ch099781305078444 ppt ch09
9781305078444 ppt ch09
 
9781305078444 ppt ch08
9781305078444 ppt ch089781305078444 ppt ch08
9781305078444 ppt ch08
 
9781305078444 ppt ch07
9781305078444 ppt ch079781305078444 ppt ch07
9781305078444 ppt ch07
 
9781305078444 ppt ch06
9781305078444 ppt ch069781305078444 ppt ch06
9781305078444 ppt ch06
 
9781305078444 ppt ch05
9781305078444 ppt ch059781305078444 ppt ch05
9781305078444 ppt ch05
 
9781305078444 ppt ch04
9781305078444 ppt ch049781305078444 ppt ch04
9781305078444 ppt ch04
 
9781305078444 ppt ch03
9781305078444 ppt ch039781305078444 ppt ch03
9781305078444 ppt ch03
 
9781305078444 ppt ch02
9781305078444 ppt ch029781305078444 ppt ch02
9781305078444 ppt ch02
 
9781305078444 ppt ch01
9781305078444 ppt ch019781305078444 ppt ch01
9781305078444 ppt ch01
 
9781337102087 ppt ch13
9781337102087 ppt ch139781337102087 ppt ch13
9781337102087 ppt ch13
 
9781337102087 ppt ch18
9781337102087 ppt ch189781337102087 ppt ch18
9781337102087 ppt ch18
 
9781337102087 ppt ch17
9781337102087 ppt ch179781337102087 ppt ch17
9781337102087 ppt ch17
 
9781337102087 ppt ch16
9781337102087 ppt ch169781337102087 ppt ch16
9781337102087 ppt ch16
 
9781337102087 ppt ch15
9781337102087 ppt ch159781337102087 ppt ch15
9781337102087 ppt ch15
 
9781337102087 ppt ch14
9781337102087 ppt ch149781337102087 ppt ch14
9781337102087 ppt ch14
 
9781337102087 ppt ch12
9781337102087 ppt ch129781337102087 ppt ch12
9781337102087 ppt ch12
 
9781337102087 ppt ch11
9781337102087 ppt ch119781337102087 ppt ch11
9781337102087 ppt ch11
 

Kürzlich hochgeladen

Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
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 17Celine George
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 

Kürzlich hochgeladen (20)

Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
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
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
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"
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 

9781285852744 ppt ch15

  • 2. Objectives • In this chapter, you will: – Learn about recursive definitions – Explore the base case and the general case of a recursive definition – Discover what a recursive algorithm is – Learn about recursive functions 2C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 3. Objectives (cont’d.) – Become familiar with direct and indirect recursion – Explore how to use recursive functions to implement recursive algorithms – Become aware of recursion vs. iteration 3C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 4. Recursive Definitions • Recursion: solving a problem by reducing it to smaller versions of itself – Provides a powerful way to solve certain problems which would be complicated otherwise 4C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 5. Recursive Definitions (cont’d.) • Recursive definition: defining a problem in terms of a smaller version of itself • Base case: the case for which the solution is obtained directly – Every recursive definition must have one (or more) base case(s) – The base case stops the recursion • General case: must eventually reduce to a base case 5C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 6. Recursive Definitions (cont’d.) • Example: factorials 0! = 1 (1) n! = n x (n-1)! if n > 0 (2) – Equation (1) is called the base case – Equation (2) is called the general case 6C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 7. Recursive Definitions (cont’d.) • Recursive algorithm: finds a solution by reducing problem to smaller versions of itself – Must have one (or more) base cases – General solution must eventually reduce to a base case • Recursive function: a function that calls itself • Recursive algorithms are implemented using recursive functions 7C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 8. Recursive Definitions (cont’d.) • Think of a recursive function as having infinitely many copies of itself – Every call has its own code and its own set of parameters and local variables – After completing a particular recursive call: • Control goes back to the calling environment, the previous call • Execution begins from the point immediately following the recursive call 8C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 9. Direct and Indirect Recursion • Directly recursive: a function that calls itself • Indirectly recursive: a function that calls another function and eventually results in the original function call • Tail recursive function: recursive function in whose last statement executed is the recursive call 9C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 10. Infinite Recursion • Infinite recursion: every recursive call results in another recursive call – In theory, infinite recursion executes forever • Because computer memory is finite: – Function executes until the system runs out of memory – Results in an abnormal program termination 10C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 11. Infinite Recursion (cont’d.) • To design a recursive function: – Understand problem requirements – Determine limiting conditions – Identify base cases and provide a direct solution to each base case – Identify general cases and provide a solution to each general case in terms of smaller versions of itself 11C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 12. Recursion or Iteration? • Iterative control structure: uses a loop to repeat a set of statements • There are usually two ways to solve a particular problem: – Iteration (looping) – Recursion • When choosing, must consider: – Nature of the problem – Efficiency 12C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 13. Recursion or Iteration? (cont’d.) • Whenever a function is called – Memory space for its formal parameters and (automatic) local variables is allocated • When the function terminates – That memory space is then deallocated • Every (recursive) call has its own set of parameters and (automatic) local variables 13C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 14. Recursion or Iteration? (cont’d.) • Overhead associated with executing a (recursive) function in terms of: – Memory space – Computer time • A recursive function executes more slowly than its iterative counterpart • Today’s computers are fast – Overhead of a recursion function is not noticeable 14C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 15. Recursion or Iteration? (cont’d.) • Sometimes iterative solution is more obvious and easier to understand • If the definition of a problem is inherently recursive, consider a recursive solution 15C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 16. Summary • Recursion: process of solving a problem by reducing it to smaller versions of itself • Recursive definition: defines a problem in terms of smaller versions of itself – Has one or more base cases • Recursive algorithm: solves a problem by reducing it to smaller versions of itself – Has one or more base cases 16C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 17. Summary (cont’d.) • The solution to the problem in a base case is obtained directly • Recursive function: function that calls itself – Must have one or more base cases • Recursive algorithms are implemented using recursive functions • The general solution breaks the problem into smaller versions of itself 17C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 18. Summary (cont’d.) • The general case must eventually be reduced to a base case – The base case stops the recursion • Directly recursive: a function calls itself • Indirectly recursive: a function calls another function and eventually calls the original • Tail recursive: the last statement executed is the recursive call 18C++ Programming: From Problem Analysis to Program Design, Seventh Edition