1 / 7

Introduction to Computer Science: Recursion and Methods Calls

This lecture introduces the concept of recursion in computer science and discusses the benefits of method calls. Examples and explanations are given to demonstrate the use of recursion and the difference between recursion and iteration.

taniar
Download Presentation

Introduction to Computer Science: Recursion and Methods Calls

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CompSci 101Introduction to Computer Science November 25, 2014 Prof. Rodger

  2. Announcements • No Reading or RQ for next time • Assignment 8 out – due Dec 4 • Assignment 9 out – due Dec 5 (extra) • APT 10 out and due – Dec 5 • Lab 11 next week • Finish lecture notes from last time

  3. Recursion • Method calls a clone of itself • Solves a problem by solving smaller subproblems • “looping” by recursive calls • CAUTION – don’t add a loop, it is implicit CompSci 100e, Spring2011

  4. Examples: recursionMisc.py • Calculates and prints the sum of integers from a list that are even • Print the numbers one per line • Mystery recursion CompSci 100e, Spring2011

  5. Recursion (more) • Watch out for infinite recursion • No way out, what happens? • Segmentation fault, out of memory • Rules • Base case (way out) – no recursive call • Recursive call(s) – solve a smaller problem CompSci 100e, Spring2011

  6. Recursion vs IterationWhich method do you use? • Iteration • Easier to define • Faster – recursion takes some overhead • Recursion • Easier to define • Shorter code CompSci 100e, Spring2011

  7. Types of Recursion • Tail recursion • One recursive call at the end of a method • Easy to replace with a loop • Reverse something • One recursive call “before”process • Multiple Recursion • More than one recursive call CompSci 100e, Spring2011

More Related