1 / 24

Introduction to Subprograms and Recursion

This week's lesson covers subprograms in programming, including how they work, parameters, and recursion. Learn how to break down complex programs into smaller tasks and improve code organization.

Download Presentation

Introduction to Subprograms and Recursion

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. Week 2: Introduction • Welcome to those who were not here last week • Last week we discussed • Computer hardware and software • Programming languages • Machine language • High-level languages • Features of high-level languages • Variables (single and arrays) • Operations on variables University of Pittsburgh Computer Science

  2. Week 2: Introduction • Control structures • Example programs using a Stack type • Push on the top • Pop from the top • We finished the implementation University of Pittsburgh Computer Science

  3. Week 2: Introduction • How can we improve our HLLs? • Complex programs can get HUGE • Millions of lines of code • This can lead to some problems • The human mind works best when a problem is not too large • Ex. SSN, phone numbers, SIMON, shopping lists • Many large projects require a team of programmers • How to divide the work, check the results? • Programs' specs sometime change • How will change affect the rest of the code? • I may have to do the same thing in different places • Why rewrite the code? (ex. walk the dog) University of Pittsburgh Computer Science

  4. Week 2: Subprograms • Solution: break our programs into SUBPROGRAMS • A group of declarations and instructions that functions similarly to a program, but that cannot run by itself • How do subprograms work? • We DECLARE them somewhere in our code • We CALL them when we want them to execute University of Pittsburgh Computer Science

  5. Week 2: Subprograms • When a subprogram is called: • The code for the subprogram executes • This may be in a different place than the main program code • When a subprogram finishes: • The main program resumes at the next instruction after the subprogram call University of Pittsburgh Computer Science

  6. Week 2: Subprograms • Real life example: Let's see what I need to do today... Feed the cat Water the garden Play tennis Fix the sidewalk Water ski Feed the cat University of Pittsburgh Computer Science

  7. Week 2: Subprograms • What does each of these really mean? Water the garden: • Unwind hose • Turn on water • Spray until wet • Turn off water • Wind up hose Feed the cat: • Open food • Put some into dish • Refill water • Reseal food University of Pittsburgh Computer Science

  8. Week 2: Subprograms • What we are doing is REFINING our tasks • At the highest level we only are concerned with what the tasks are • When doing the tasks we need to know each step involved • We do the same thing with programs • From main program we just need to know the subprograms called • Within the subprograms we have the precise instructions that need to execute University of Pittsburgh Computer Science

  9. Week 2: Subprograms • Let's look at a simple example • Download, compile and run Seuss1.java from the Exercises page University of Pittsburgh Computer Science

  10. Week 2: Parameters • Usually we need our subprograms to communicate with the main program • Ex. Building a house • General contractor has subcontractors to do the electrical, plumbing, masonry, etc. • General contractor needs to talk to the subcontractors regarding scheduling, any problems, etc. • Something learned from one subcontractor may affect how another subcontractor works • Pipes have been moved • Subfloor is not installed yet • We accomplish this communication in programs with PARAMETERS University of Pittsburgh Computer Science

  11. Week 2: Parameters • Parameters • We can think of these as messages that are passed from the main to the subs (and possibly back to the main) • When a subprogram is called, the parameters passed to the subprogram are called ARGUMENTS • The subprogram uses these to help do it's job • See Seuss2.java • Download from Exercises page • Compile and run it • Add another verse to it University of Pittsburgh Computer Science

  12. Week 2: How Subprograms Work • When a subprogram is called • Arguments are passed to it • Data for it is pushed onto the RUN-TIME STACK • Stack works just as we discussed last week • Code for subprogram starts to execute • Why do we need a stack for the data? • A subprogram can call another subprogram • We need to keep track of the data for each subprogram that has been called • When "top" subprogram finishes, the next one down (new top) should resume University of Pittsburgh Computer Science

  13. Week 2: Recursion • The run-time stack also allows us to use RECURSION in our programs • A subprogram calls itself • Quite mathematical in nature -- some common math problems can be defined recursively • N! • Common solution: N! = N x (N-1) x (N-2) … x 1 • Recursive solution: N! = N x (N-1)! when N > 0 recursive case N! = 1 when N = 0 base case University of Pittsburgh Computer Science

  14. Week 2: Recursion • NM • Common solution: NM = N x N x N x … x N -> M times • Recursive solution: NM = N x N(M-1) when M > 0 recursive case NM = 1 when M = 0 base case • Concept seems difficult • But can be useful • Some famous algorithms are defined recursively • Sorting (QuickSort, MergeSort) • Searching (Binary Search) • Image processing (Fast-Fourier Transform) University of Pittsburgh Computer Science

  15. Week 2: Recursion • Let's look at another famous example • Not very useful, but famous anyway • Good example of a problem that is VERY DIFFICULT to solve WITHOUT recursion • Towers of Hanoi problem • We have 3 towers • On first tower we have disks of decreasing size • Goal is to get all disks onto last tower, but • We can only move one disk at a time • We can never put a larger disk on top of a smaller one University of Pittsburgh Computer Science

  16. Week 2: Recursion • Download Hanoi.java from Web site • Compile it and then run it • First try playing it yourself for a while • Start with only 3 or 4 disks, then try more • Then let the computer solve it for you • You can go up to 10 disks • Adjust the delay so you can see each step for just a few disks (make it 1000 or more) • For more disks make the delay small (50 or less) so the demo will finish in a reasonable amount of time • When you are done playing, we will look at the code for a bit University of Pittsburgh Computer Science

  17. Week 2: Subprogram Summary • In summary, subprograms give us • Smaller code pieces • Each piece not so complex • Relatively independent pieces • Easier to debug them • Easier to add new pieces and to change the old ones without "breaking" the rest of them • Easier to teams to work on different pieces • Reusable pieces • I can call a subprogram many times from the same program • I can use the same subprogram in many programs University of Pittsburgh Computer Science

  18. Week 2: Subprogram Summary • Procedural abstraction • What is that? • Idea: I can use a subprogram without necessarily knowing the details of how it works • I should probably know its input, output and its general purpose, but that's all • Ex: y = sqrt(x) • Calculate the square root • I know what that is • I know it is a real number • I don't care how the computer actually does it! • Unless I was the one who had to write the code University of Pittsburgh Computer Science

  19. Week 2: Data Abstraction and OOP • We can expand this idea to include DATA ABSTRACTION • I don't need to know the implementation details of a data type in order to use it • Ex. Stack from last week • If we know what push and pop MEAN, we don't really have to know how they are implemented • In fact a Stack can be implemented in various ways • We only saw one of them last week • No matter how it is implemented, it still functions as a Stack in the same way • Data abstraction is one important part of OBJECT-ORIENTED PROGRAMMING University of Pittsburgh Computer Science

  20. Week 2: Object-Oriented Programming • OOP components • Encapsulation/Data Abstraction • The DATA and OPERATIONS for a type are encapsulated together into an OBJECT • Ex: Stack • Operations: Push, Pop (and others) • Data: array of integers (or other data) • Ex: Person • Operations: Walk, talk, eat, sleep • Data: body, name, address, ssn • Look at Hanoi.java again • class Tower University of Pittsburgh Computer Science

  21. Week 2: Object-Oriented Programming • Inheritance • Allows new classes to get all of the attributes of previously defined classes • Allows related classes to be accessed in a consistent way Animal Mammal University of Pittsburgh Computer Science

  22. Week 2: Object-Oriented Programming • Polymorphism • Allows the same operation name to be defined differently for different classes Animal Mammal move() move() move() move() move() University of Pittsburgh Computer Science

  23. Week 2: Object-Oriented Programming • We may look at object-oriented programming in more detail in a lab session later University of Pittsburgh Computer Science

  24. Week 2: Summary • This week we discussed • Reasons for using subprograms • How subprograms work • Calling a subprogram • Parameters • Recursion • Wacky! • Object-oriented Programming • Encapsulation • Inheritance • Polymorphism University of Pittsburgh Computer Science

More Related