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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

86 <strong>Computer</strong> <strong>Programming</strong> <strong>Concepts</strong> <strong>and</strong> <strong>Visual</strong> <strong>Basic</strong><br />

EXAMPLE 7<br />

The following program demonstrates the use of Form_Load.<br />

Dim pi As Single ‘In (Declarations) section of (General)<br />

Private Sub Form_Load()<br />

‘Assign a value to pi<br />

pi = 3.14159<br />

End Sub<br />

Private Sub cmdCompute_Click()<br />

‘Display the area of a circle of radius 5<br />

picArea.Cls<br />

picArea.Print “The area of a circle of radius 5 is”; pi * 5 * 5<br />

End Sub<br />

[Run, <strong>and</strong> then click the comm<strong>and</strong> button. The following is displayed in the picture box.]<br />

The area of a circle of radius 5 is 78.53975<br />

COMMENTS<br />

1. In addition to the reasons presented earlier, some other reasons for using Sub<br />

procedures follow:<br />

(a) Programs with Sub procedures are easier to debug. Each Sub procedure can<br />

be checked individually before being placed into the program.<br />

(b) The task performed by a Sub procedure might be needed in another program.<br />

The Sub procedure can be reused with no changes. Programmers refer<br />

to the collection of their most universal Sub procedures as a library of Sub<br />

procedures. (The fact that variables appearing in Sub procedures are local to<br />

the Sub procedures is quite helpful when reusing Sub procedures in other<br />

programs. There is no need to worry if a variable name in the Sub procedure<br />

is used for a different purpose in another part of the program.)<br />

(c) Often, programs are written by a team of programmers. After a problem has<br />

been broken into distinct <strong>and</strong> manageable tasks, each programmer is<br />

assigned a single Sub procedure to write.<br />

(d) Sub procedures make large programs easier to underst<strong>and</strong>. Some programming<br />

st<strong>and</strong>ards insist that each Sub procedure be at most two pages long.<br />

(e) Sub procedures permit the following program design, which provides a<br />

built-in outline of an event procedure. A reader can focus on the main flow<br />

first, <strong>and</strong> then go into the specifics of accomplishing the secondary tasks.<br />

Private Sub Object_Event()<br />

‘An event procedure written entirely as Sub procudures<br />

Call FirstSubprocedure ‘Perform first task<br />

Call SecondSubprocedure ‘Perform second task<br />

Call ThirdSubprocedure ‘Perform third task<br />

End Sub<br />

2. Sub procedures can call other Sub procedures. In such cases, the calling Sub<br />

procedure plays the role of the event procedure with respect to the called Sub<br />

procedure. Complex problems are thereby broken into simpler tasks, which are<br />

then broken into still more elementary tasks. This approach to problem solving<br />

is called top-down design.

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

Saved successfully!

Ooh no, something went wrong!