Designing with procedures
This presentation is the property of its rightful owner.
Sponsored Links
1 / 8

Designing with Procedures PowerPoint PPT Presentation


  • 90 Views
  • Uploaded on
  • Presentation posted in: General

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

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Designing with procedures

Designing with Procedures


Designing a program 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

  • Look for opportunities to use a procedure to avoid duplication of code


Information sharing

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!


Procedure specification

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!


Implementation

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.


Refactoring

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


Example

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.


Demo ice cream order with procedures

Demo: Ice Cream Order with Procedures


  • Login