1 / 8

Designing with Procedures

Designing with Procedures. Designing a Program with Procedures. If the code for your program is going to be less than one page, normally don’t bother; otherwise: List the major steps of the program. See if it makes sense to use a procedure for each major step

mirit
Download Presentation

Designing with Procedures

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. Designing with Procedures

  2. Designing a Program with Procedures • If the code for your program is going to be less than one page, normally don’t bother; otherwise: • List the major steps of the program. See if it makes sense to use a procedure for each major step • Look for opportunities to use a procedure to avoid duplication of code

  3. Information Sharing • What information does each procedure need? • What should the formal parameters be? • What should the global variables be? • What should be handled by local variables? • Keep in mind that variables should be local unless there is a very good reason to make them global!

  4. Procedure Specification • For each procedure in your design, write a brief description of what it does, what global variables it changes, and what parameters it needs. If it is a function, include what value it returns • Step through an execution of your process and make sure the information flow is correct • Remember to develop tests before you write code!

  5. Implementation • DO NOT WRITE THE WHOLE PROGRAM AT ONCE!!!! • You can write just one procedure, or just parts of a couple of procedures, and test them. Keep adding bits using your design as a guide until you have all the functionality. • Write enough printing code as you go along to see what’s happening. If you have to write extra code, it will save you time in the long run. Or, step through.

  6. Refactoring • Rewriting existing code to improve it is a common operation • Often features are added to a program incrementally and at some point it just gets too messy, unwieldy, and hard to understand • Changing and improving the code structure is called refactoring

  7. Example • We’ll redo the IceCreamV2 program using procedures. Your Assignment 4 is to do the same thing with your Assignment 3. • Or, if you want to start fresh, you can adapt an alternative program, which is posted in the Week 4 folder • A version of the procedurizedIce Cream program is posted on the website in the Week 4 folder.

  8. Demo: Ice Cream Order with Procedures

More Related