19.12.2012 Views

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

The meaningful procedure names, along with relevant comments, identify the<br />

tasks performed by the modules.<br />

The meaningful variable names help the programmer to recall the purpose of<br />

each variable.<br />

4. Easy to change.<br />

Because a structured program is self-documenting, it can easily be deciphered<br />

by another programmer.<br />

Modifying a structured program often amounts to inserting or altering a few<br />

procedures rather than revising an entire complex program. The programmer<br />

does not even have to look at most of the program. This is in sharp contrast to<br />

the situation with unstructured programs that require an underst<strong>and</strong>ing of the<br />

entire logic of the program before any changes can be made with confidence.<br />

SUMMARY<br />

1. A general procedure is a portion of a program that is accessed by event procedures<br />

or other general procedures. The two types of general procedures are Sub<br />

procedures <strong>and</strong> Function procedures.<br />

2. Sub procedures are defined in blocks beginning with Sub statements <strong>and</strong> ending<br />

with End Sub statements. They are accessed by Call statements.<br />

3. Function procedures are defined in blocks beginning with Function statements<br />

<strong>and</strong> ending with End Function statements. A function is activated by a reference<br />

in an expression <strong>and</strong> returns a value.<br />

4. In any procedure, the arguments appearing in the calling statement must match<br />

the parameters of the Sub or Function statement in number, type, <strong>and</strong> order.<br />

They need not match in name.<br />

5. A variable declared in the (Declarations) section of (General) is form-level.<br />

Such a variable is available to every procedure in the form’s code <strong>and</strong> retains its<br />

value from one procedure invocation to the next. Form-level variables are often<br />

initialized in the Form_Load event procedure.<br />

6. A variable appearing inside a procedure is local to the procedure if it is declared<br />

in a Dim statement within the procedure or if it is not a form-level variable <strong>and</strong><br />

does not appear in the parameter list. The values of these variables are reinitialized<br />

each time the procedure is called. A variable with the same name<br />

appearing in another part of the program is treated as a different variable.<br />

7. Structured programming uses modular design to refine large problems into<br />

smaller subproblems. Programs are coded using the three logical structures of<br />

sequences, decisions, <strong>and</strong> loops.<br />

PROGRAMMING PROJECTS<br />

1. The numbers of calories per gram of carbohydrate, fat, <strong>and</strong> protein are 4, 9, <strong>and</strong><br />

4, respectively. Write a program that requests the nutritional content of a 1ounce<br />

serving of food <strong>and</strong> displays the number of calories in the serving. The<br />

input <strong>and</strong> output should be h<strong>and</strong>led by Sub procedures <strong>and</strong> the calories computed<br />

by a function. A sample run for a typical breakfast cereal is shown in<br />

Figure 3-8.<br />

<strong>Programming</strong> Projects 97

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!