0% found this document useful (0 votes)
7 views1 page

Understanding Recursive Methods

A recursive method is one that calls itself, directly or indirectly. For recursion to work successfully, each recursive call must simplify the problem and there must be base cases to handle the simplest problems. Recursion involves dividing a problem into smaller pieces, solving the simplest cases and recursively calling itself on the smaller problems until reaching the base cases. This results in repetition through repeated method calls that eventually converge on the base cases to terminate recursion and return the final result.

Uploaded by

SaihielBakshi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views1 page

Understanding Recursive Methods

A recursive method is one that calls itself, directly or indirectly. For recursion to work successfully, each recursive call must simplify the problem and there must be base cases to handle the simplest problems. Recursion involves dividing a problem into smaller pieces, solving the simplest cases and recursively calling itself on the smaller problems until reaching the base cases. This results in repetition through repeated method calls that eventually converge on the base cases to terminate recursion and return the final result.

Uploaded by

SaihielBakshi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Arecursivemethodisamethodthatcallsitselfeitherdirectlyor

indirectly
Therearetwokeyrequirementstomakesurethattherecursionissuccessful:
Everyrecursivecallmustsimplifythecomputationinsomeway.
Theremustbespecialcasestohandlethesimplestcomputations.
[Link]
Ifarecursivemethodiscalledwithabasecase,themethodreturnsaresult.
Ifamethodiscalledwithamorecomplexproblem,themethoddividesthe
problemintotwoormoreconceptualpieces:apiecethatthemethodknows
[Link]
newproblemlooksliketheoriginalproblem,themethodlaunchesarecursive
calltoworkonthesmallerproblem.
Forrecursiontoterminate,eachtimetherecursionmethodcallsitselfwitha
slightlysimplerversionoftheoriginalproblem,thesequenceofsmallerand
[Link]
recognizesthebasecase,theresultisreturnedtothepreviousmethodcall
andasequenceofreturnsensuresallthewayupthelineuntiltheoriginal
callofthemethodeventuallyreturnsthefinalresult.
Bothiterationandrecursionarebasedonacontrolstructure:Iterationuses
arepetitionstructurerecursionusesaselectionstructure.
Bothiterationandrecursioninvolverepetition:Iterationexplicitlyusesa
repetitionstructurerecursionachievesrepetitionthroughrepeatedmethod
calls.
Iterationandrecursioneachinvolveaterminationtest:Iterationterminates
whentheloopcontinuationconditionfailsrecursionterminateswhenabase
caseisrecognized.
Iterationandrecursioncanoccurinfinitely:Aninfiniteloopoccurswith
iterationiftheloopcontinuationtestneverbecomesfalseinfiniterecursion
occursiftherecursionstepdoesnotreducetheprobleminamannerthat
convergesonthebasecase.
Recursionrepeatedlyinvokesthemechanism,andconsequentlythe
overhead,[Link]
memoryspace.

You might also like