SlideShare uma empresa Scribd logo
1 de 7
Recursion
What is recursion
• Looping without a loop statement

• A function that is part of its own
  definition

• Can only work if there’s a terminating
  condition, otherwise it goes forever
  (the base case)
A recursive function
• N Factorial
• 1!   =   1
  2!   =   1   x   2   =   2
  3!   =   1   x   2   x   3 = 2! x 3 = 6
  N!   =   1   x   2   x   3 x .... (N-2) x (N-1) x N = (N-1)! x N
How recursion is handled
• Every time a function is called, the
  function values, local variables,
  parameters and return addresses are
  pushed onto the stack.

• Over and Over again
• You might run out!
Dry-running a recursive call
Procedure Printsequence(n)
    n <- n-1
    if n > 1 then
       Printsequence(n)
    endif
    output(n)
EndProcedure
Dry-running a recursive call
Procedure Printsequence(n)
    n <- n-1
    if n > 1 then
       output(n)
       Printsequence(n)
    endif

EndProcedure
How to write a recursive
         procedure
• Code the general case
  Result = n * Factorial(n-1)
• Code the base case
  Result = 1
• Ensure that the base case is reached
  after a finite number of recursive calls

Mais conteúdo relacionado

Semelhante a Recursion

Recurrent Neural Networks (RNNs)
Recurrent Neural Networks (RNNs)Recurrent Neural Networks (RNNs)
Recurrent Neural Networks (RNNs)
Abdullah al Mamun
 
Playing Go with Clojure
Playing Go with ClojurePlaying Go with Clojure
Playing Go with Clojure
ztellman
 
Presentation of GetTogether on Functional Programming
Presentation of GetTogether on Functional ProgrammingPresentation of GetTogether on Functional Programming
Presentation of GetTogether on Functional Programming
Filip De Sutter
 
Advanced functions ppt (Chapter 1) part i
Advanced functions ppt (Chapter 1) part iAdvanced functions ppt (Chapter 1) part i
Advanced functions ppt (Chapter 1) part i
Tan Yuhang
 
Advanced functions part i
Advanced functions part iAdvanced functions part i
Advanced functions part i
Xin Wei
 
cos323_s06_lecture03_optimization.ppt
cos323_s06_lecture03_optimization.pptcos323_s06_lecture03_optimization.ppt
cos323_s06_lecture03_optimization.ppt
devesh604174
 
PowerPoint Presentation
PowerPoint PresentationPowerPoint Presentation
PowerPoint Presentation
butest
 
PowerPoint Presentation
PowerPoint PresentationPowerPoint Presentation
PowerPoint Presentation
butest
 

Semelhante a Recursion (20)

6-Python-Recursion PPT.pptx
6-Python-Recursion PPT.pptx6-Python-Recursion PPT.pptx
6-Python-Recursion PPT.pptx
 
Recurrent Neural Networks (RNNs)
Recurrent Neural Networks (RNNs)Recurrent Neural Networks (RNNs)
Recurrent Neural Networks (RNNs)
 
RECURSION.pptx
RECURSION.pptxRECURSION.pptx
RECURSION.pptx
 
RecursionWeek8.ppt
RecursionWeek8.pptRecursionWeek8.ppt
RecursionWeek8.ppt
 
Playing Go with Clojure
Playing Go with ClojurePlaying Go with Clojure
Playing Go with Clojure
 
Presentation of GetTogether on Functional Programming
Presentation of GetTogether on Functional ProgrammingPresentation of GetTogether on Functional Programming
Presentation of GetTogether on Functional Programming
 
Searching using Quantum Rules
Searching using Quantum RulesSearching using Quantum Rules
Searching using Quantum Rules
 
Advanced functions ppt (Chapter 1) part i
Advanced functions ppt (Chapter 1) part iAdvanced functions ppt (Chapter 1) part i
Advanced functions ppt (Chapter 1) part i
 
Advanced functions part i
Advanced functions part iAdvanced functions part i
Advanced functions part i
 
cos323_s06_lecture03_optimization.ppt
cos323_s06_lecture03_optimization.pptcos323_s06_lecture03_optimization.ppt
cos323_s06_lecture03_optimization.ppt
 
Applied Deep Learning 11/03 Convolutional Neural Networks
Applied Deep Learning 11/03 Convolutional Neural NetworksApplied Deep Learning 11/03 Convolutional Neural Networks
Applied Deep Learning 11/03 Convolutional Neural Networks
 
PowerPoint Presentation
PowerPoint PresentationPowerPoint Presentation
PowerPoint Presentation
 
PowerPoint Presentation
PowerPoint PresentationPowerPoint Presentation
PowerPoint Presentation
 
Recursion
RecursionRecursion
Recursion
 
Pseudo and Quasi Random Number Generation
Pseudo and Quasi Random Number GenerationPseudo and Quasi Random Number Generation
Pseudo and Quasi Random Number Generation
 
Optim_methods.pdf
Optim_methods.pdfOptim_methods.pdf
Optim_methods.pdf
 
Complexity Analysis of Recursive Function
Complexity Analysis of Recursive FunctionComplexity Analysis of Recursive Function
Complexity Analysis of Recursive Function
 
Lecture 1 Signals.pdf
Lecture 1 Signals.pdfLecture 1 Signals.pdf
Lecture 1 Signals.pdf
 
Recursion.pdf
Recursion.pdfRecursion.pdf
Recursion.pdf
 
Tower of Hanoi.ppt
Tower of Hanoi.pptTower of Hanoi.ppt
Tower of Hanoi.ppt
 

Mais de grahamwell (20)

Pseudocode
PseudocodePseudocode
Pseudocode
 
Excel =if function
Excel =if functionExcel =if function
Excel =if function
 
Excel Min max-average
Excel Min max-average Excel Min max-average
Excel Min max-average
 
What is binary and why do we use it?
What is binary and why do we use it?What is binary and why do we use it?
What is binary and why do we use it?
 
Introduction to touch develop
Introduction to touch developIntroduction to touch develop
Introduction to touch develop
 
Introduction to touch develop
Introduction to touch developIntroduction to touch develop
Introduction to touch develop
 
The software story
The software storyThe software story
The software story
 
Turtle graphics
Turtle graphicsTurtle graphics
Turtle graphics
 
Database field types
Database field typesDatabase field types
Database field types
 
Databases 101
Databases 101Databases 101
Databases 101
 
Kodu controls
Kodu controlsKodu controls
Kodu controls
 
Pascal names and types
Pascal names and typesPascal names and types
Pascal names and types
 
Python part two names and types
Python part two names and typesPython part two names and types
Python part two names and types
 
Abstraction - Year 9
Abstraction - Year 9Abstraction - Year 9
Abstraction - Year 9
 
Thinking about your project
Thinking about your projectThinking about your project
Thinking about your project
 
The rail fence
The rail fenceThe rail fence
The rail fence
 
Lesson 1
Lesson 1Lesson 1
Lesson 1
 
Rsa encryption
Rsa encryptionRsa encryption
Rsa encryption
 
Server side scripts
Server side scriptsServer side scripts
Server side scripts
 
Revision topic 1 sensors and control
Revision topic 1 sensors and controlRevision topic 1 sensors and control
Revision topic 1 sensors and control
 

Recursion

  • 2. What is recursion • Looping without a loop statement • A function that is part of its own definition • Can only work if there’s a terminating condition, otherwise it goes forever (the base case)
  • 3. A recursive function • N Factorial • 1! = 1 2! = 1 x 2 = 2 3! = 1 x 2 x 3 = 2! x 3 = 6 N! = 1 x 2 x 3 x .... (N-2) x (N-1) x N = (N-1)! x N
  • 4. How recursion is handled • Every time a function is called, the function values, local variables, parameters and return addresses are pushed onto the stack. • Over and Over again • You might run out!
  • 5. Dry-running a recursive call Procedure Printsequence(n) n <- n-1 if n > 1 then Printsequence(n) endif output(n) EndProcedure
  • 6. Dry-running a recursive call Procedure Printsequence(n) n <- n-1 if n > 1 then output(n) Printsequence(n) endif EndProcedure
  • 7. How to write a recursive procedure • Code the general case Result = n * Factorial(n-1) • Code the base case Result = 1 • Ensure that the base case is reached after a finite number of recursive calls