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.

preceded with the word ByVal. Built-in functions have all their arguments<br />

passed by value. Some programmers feel that “by value” should have been the<br />

default for Function procedures, rather than “by reference.”<br />

2. Function procedures can perform the same tasks as Sub procedures. For<br />

instance, they can request input <strong>and</strong> display text; however, they are primarily<br />

used to calculate a single value. Normally, Sub procedures are used to carry out<br />

other tasks.<br />

3. Function procedures differ from Sub procedures in the way they are accessed.<br />

Sub procedures are invoked with Call statements, whereas functions are<br />

invoked by placing them where you would otherwise expect to find a constant,<br />

variable, or expression. Unlike a Function procedure, a Sub procedure can’t be<br />

used in an expression.<br />

4. Function procedures can invoke other Function procedures or Sub procedures.<br />

5. Function procedures, like Sub procedures need not have any parameters. Unlike<br />

Sub procedures, when a parameterless function is used, the function name may<br />

be followed by an empty set of parentheses. The following program uses a<br />

“parameterless” function.<br />

Private Sub cmdButton_Click()<br />

‘Request <strong>and</strong> display a saying<br />

picBox.Cls<br />

picBox.Print Saying() ‘() is optional<br />

End Sub<br />

Private Function Saying() As String<br />

‘Retrieve a saying from the user<br />

Saying = InputBox(“What is your favorite saying?”)<br />

End Function<br />

[Run, click the comm<strong>and</strong> button, <strong>and</strong> then type Less is more. into the message<br />

box.]<br />

The saying Less is more. is displayed in the picture box.<br />

6. An alternative method of creating a Function procedure is to move the cursor to<br />

a blank line outside of any procedure, type Private Function FunctionName, <strong>and</strong><br />

press the Enter key.<br />

3.4 MODULAR DESIGN<br />

■ TOP-DOWN DESIGN<br />

Large problems usually require large programs. One method programmers use to make a large<br />

problem more underst<strong>and</strong>able is to divide it into smaller, less complex subproblems. Repeatedly<br />

using a “divide-<strong>and</strong>-conquer” approach to break up a large problem into smaller subproblems<br />

is called stepwise refinement. Stepwise refinement is part of a larger methodology of writing<br />

programs known as top-down design. The term top-down refers to the fact that the more<br />

general tasks occur near the top of the design <strong>and</strong> tasks representing their refinement occur<br />

below. Top-down design <strong>and</strong> structured programming emerged as techniques to enhance programming<br />

productivity. Their use leads to programs that are easier to read <strong>and</strong> maintain. They<br />

also produce programs containing fewer initial errors, with these errors being easier to find<br />

<strong>and</strong> correct. When such programs are later modified, there is a much smaller likelihood of introducing<br />

new errors.<br />

The goal of top-down design is to break a problem into individual tasks, or modules, that<br />

can easily be transcribed into pseudocode, flowcharts, or a program. First, a problem is<br />

restated as several simpler problems depicted as modules. Any modules that remain too complex<br />

are broken down further. The process of refining modules continues until the smallest<br />

Modular Design 93

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

Saved successfully!

Ooh no, something went wrong!