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.

3.1 SUB PROCEDURES, PART I<br />

Structured program design requires that problems be broken into small problems to be solved<br />

one at a time. <strong>Visual</strong> <strong>Basic</strong> has two devices, Sub procedures <strong>and</strong> Function procedures, that<br />

are used to break problems into manageable chunks. To distinguish them from event procedures,<br />

Sub <strong>and</strong> Function procedures are referred to as general procedures. General procedures also<br />

eliminate repetitive code, can be reused in other programs, <strong>and</strong> allow a team of programmers<br />

to work on a single program.<br />

In this section we show how Sub procedures are defined <strong>and</strong> used. The programs in this<br />

section are designed to demonstrate the use of Sub procedures rather than to accomplish<br />

sophisticated programming tasks. Later chapters of the book use them for more substantial<br />

programming efforts.<br />

A Sub procedure is a part of a program that performs one or more related tasks, has its<br />

own name, is written as a separate part of the program. The simplest type of Sub procedure<br />

has the form<br />

Private Sub ProcedureName()<br />

statement(s)<br />

End Sub<br />

A Sub procedure is invoked with a statement of the form<br />

Call ProcedureName<br />

The rules for naming general procedures are identical to the rules for naming variables.<br />

The name chosen for a Sub procedure should describe the task it performs. Sub procedures<br />

can be either typed directly into the code window, or into a template created with the following<br />

steps:<br />

1. Press Alt/T/P to select Add Procedure from the Tools menu.<br />

2. Type in the name of the procedure. (Omit parentheses.)<br />

3. Select Sub from the Type box.<br />

4. Select Private from the Scope box. Note: Actually either Public or Private is<br />

OK. A Public procedure is available to all forms, whereas a Private procedure<br />

is only available to the form in which it is defined.<br />

5. Press the Enter key or click on OK.<br />

73

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

Saved successfully!

Ooh no, something went wrong!