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.

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

Consider the following program that calculates the sum of two numbers. This program<br />

will be revised to incorporate Sub procedures.<br />

Object Property Setting<br />

frmArithmetic Caption Arithmetic<br />

cmdAdd Caption Add Numbers<br />

picResult<br />

Private Sub cmdAdd_Click()<br />

Dim num1 As Single, num2 As Single<br />

‘Display the sum of two numbers<br />

picResult.Cls<br />

picResult.Print “This program displays a sentence “<br />

picResult.Print “identifying two numbers <strong>and</strong> their sum.”<br />

picResult.Print num1 = 2 num2 = 3<br />

picResult.Print “The sum of”; num1; “<strong>and</strong>”; num2; “is”; num1 + num2<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 />

This program displays a sentence<br />

identifying two numbers <strong>and</strong> their sum.<br />

The sum of 2 <strong>and</strong> 3 is 5<br />

The tasks performed by this program can be summarized as follows:<br />

Explain purpose of program.<br />

Display numbers <strong>and</strong> their sum.<br />

Sub procedures allow us to write <strong>and</strong> read the program in such a way that we first focus on<br />

the tasks <strong>and</strong> later on how to accomplish each task.<br />

EXAMPLE 1<br />

The following program uses a Sub procedure to accomplish the first task of the preceding program. When<br />

the statement Call ExplainPurpose is reached, execution jumps to the Sub ExplainPurpose statement. The<br />

lines between Sub ExplainPurpose <strong>and</strong> End Sub are executed, <strong>and</strong> then execution continues with the line<br />

following the Call statement.<br />

Private Sub cmdAdd_Click()<br />

Dim num1 As Single, num2 As Single<br />

‘Display the sum of two numbers<br />

picResult.Cls<br />

Call ExplainPurpose<br />

picResult.Print<br />

num1 = 2<br />

num2 = 3<br />

picResult.Print “The sum of”; num1; “<strong>and</strong>”; num2; “is”; num1 + num2<br />

End Sub<br />

Private Sub ExplainPurpose()<br />

‘Explain the task performed by the program<br />

picResult.Print “This program displays a sentence”<br />

picResult.Print “identifying two numbers <strong>and</strong> their sum.”<br />

End Sub

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

Saved successfully!

Ooh no, something went wrong!