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.

Call CalculateSum(x, y, s)<br />

Call DisplayResult(x, y, s)<br />

End Sub<br />

Private Sub CalculateSum(num1 As Single, num2 As Single, sum As Single)<br />

‘Add the values of num1 <strong>and</strong> num2<br />

‘<strong>and</strong> assign the value to sum<br />

sum = num1 + num2<br />

End Sub<br />

Private Sub DisplayResult(num1 As Single, num2 As Single, sum As Single)<br />

‘Display a sentence giving the two numbers <strong>and</strong> their sum<br />

picResult.Cls<br />

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

End Sub<br />

Private Sub GetNumbers(num1 As Single, num2 As Single)<br />

‘Record the two numbers in the text boxes<br />

num1 = Val(txtFirstNum.Text)<br />

num2 = Val(txtSecondNum.Text)<br />

End Sub<br />

■ PASSING BY VALUE<br />

Sometimes you want to pass a variable to a Sub procedure, but you want to ensure that the variable<br />

will retain its original value after the Sub procedure terminates—regardless of what was<br />

done to the corresponding parameter inside the Sub procedure. Such a variable is said to be<br />

passed by value. There are two ways to pass a variable by value.<br />

1. In the Call statement, enclose the variable in an extra pair of parentheses.<br />

2. In the Private Sub statement, precede the corresponding parameter with the<br />

word ByVal.<br />

For instance, in Example 1, if you change the Call statement to<br />

Call Triple((amt))<br />

then the output will be<br />

2 2 6 2<br />

The same output results if you change the Private Sub statement to<br />

Private Sub Triple(ByVal num As Single)<br />

When a variable is passed by value, two memory locations are involved. At the time the<br />

Sub procedure is called, a temporary second memory location for the parameter is set aside<br />

for the Sub procedure’s use <strong>and</strong> the value of the argument is copied into that location. After<br />

the completion of the Sub procedure, the temporary memory location is released <strong>and</strong> the<br />

value in it is lost.<br />

■ LOCAL VARIABLES<br />

Sub Procedures, Part II 83<br />

When the same variable name appears in two different Sub procedures or in a Sub procedure<br />

<strong>and</strong> an event procedure, <strong>Visual</strong> <strong>Basic</strong> gives the variables separate identities <strong>and</strong> treats them as<br />

two different variables. A value assigned to a variable in one part of the program will not affect<br />

the value of the like-named variable in the other part of the program, unless, of course,<br />

the values are passed by a Call statement. Also, each time a Sub procedure is called, all declared<br />

variables that are not parameters assume their default values. (Numeric variables have

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

Saved successfully!

Ooh no, something went wrong!