Presentation is loading. Please wait.

Presentation is loading. Please wait.

Designing with Procedures 1. Designing a Program with Procedures If the code for your program is going to be less than one page, normally don’t bother;

Similar presentations


Presentation on theme: "Designing with Procedures 1. Designing a Program with Procedures If the code for your program is going to be less than one page, normally don’t bother;"— Presentation transcript:

1 Designing with Procedures 1

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 2

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! 3

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! 4

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. 5

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 6

7 Example We’ll redo the IceCream V2 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 procedurized Ice Cream program is posted on the website in the Week 4 folder. 7

8 Demo: Ice Cream Order with Procedures


Download ppt "Designing with Procedures 1. Designing a Program with Procedures If the code for your program is going to be less than one page, normally don’t bother;"

Similar presentations


Ads by Google