Download
sub procedures passing values back from sub procedures n.
Skip this Video
Loading SlideShow in 5 Seconds..
Sub Procedures; Passing Values Back From Sub Procedures PowerPoint Presentation
Download Presentation
Sub Procedures; Passing Values Back From Sub Procedures

Sub Procedures; Passing Values Back From Sub Procedures

98 Vues Download Presentation
Télécharger la présentation

Sub Procedures; Passing Values Back From Sub Procedures

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Sub Procedures;Passing Values Back From Sub Procedures Passing by reference Passing by value

  2. Passing Arguments By Reference • Passing arguments by reference gives the procedure access to the actual variable contents in its memory address location. • The variable's value can be permanently changed by the procedure to which it is passed. • Passing by reference is the default in Visual Basic.

  3. Private Sub cmdDisplay_Click() Dim amt As Single 'Illustrate effect of value of parameter on value of argument picResults.Cls amt = 2 picResults.Print amt; Call Triple(amt) picResults.Print amt End Sub Private Sub Triple(num As Single) 'Triple a number picResults.Print num; num = 3 * num picResults.Print num; End Sub

  4. Private Sub cmdCompute_Click() Dim x As Single, y As Single 'Display the sum of the two numbers Call GetNumbers(x, y) Call Add(x, y) End Sub Private Sub Add(num1 As Single, num2 As Single) Dim sum As Single 'Display numbers and their sum picResult.Cls sum = num1 + num2 picResult.Print "The sum of"; num1; "and"; num2; "is"; sum End Sub Private Sub GetNumbers(num1 As Single, num2 As Single) 'Record the two numbers in the text boxes num1 = Val(txtFirstNum.Text) num2 = Val(txtSecondNum.Text) End Sub

  5. Passing Arguments By Value • Only a copy of a variable is passed when an argument is passed by value. • If the procedure changes the value, the change affects only the copy and not the variable itself. • Use the ByVal keyword to indicate an argument passed by value. Sub PostAccounts(ByVal intAcctNum As Integer) . . ' Place statements here. . End Sub

  6. Private Sub cmdCompute_Click() Dim x As Single, y As Single 'Display the sum of the two numbers Call GetNumbers(x, y) Call Add(x, y) End Sub Private Sub Add(ByVal num1 As Single, ByVal num2 As Single) Dim sum As Single 'Display numbers and their sum picResult.Cls sum = num1 + num2 picResult.Print "The sum of"; num1; "and"; num2; "is"; sum End Sub Private Sub GetNumbers(ByVal num1 As Single, ByVal num2 As Single) 'Record the two numbers in the text boxes num1 = Val(txtFirstNum.Text) num2 = Val(txtSecondNum.Text) End Sub

  7. Scope of Variables; Procedure-level Variables • The scope of a variable defines which parts of your code are aware of its existence. • Procedure-level variables are recognized only in the procedure in which they're declared. These are also known as local variables. • You declare procedure-level variables with the Dim or Static keywords: Dim intTemp As Integer Static intPermanent As Integer

  8. Private Sub cmdDisplay_Click() Dim x As Single x = 2 picResults.Print x; Call Trivial picResults.Print x; Call Trivial picResults.Print x; End Sub Private Sub Trivial() Dim x As Single 'Do something trivial picResults.Print x; x = 3 picResults.Print x; End Sub

  9. Private Sub cmdDisplay_Click() Dim x As Single x = 2 picResults.Print x; Call Trivial picResults.Print x; Call Trivial picResults.Print x; End Sub Private Sub Trivial() Static x As Single 'Do something trivial picResults.Print x; x = 3 picResults.Print x; End Sub

  10. Form-level Variables • A form-level variable is available to all procedures in the form. • You create form-level variables by declaring them with the Private keyword in the Declarations section at the top of the form. • When a form-level variable is assigned a value in a procedure, it retains that value when the procedure is exited.

  11. Dim num1 As Single, num2 As Single Private Sub cmdDisplay_Click() 'Display the sum of two numbers num1 = 2 num2 = 3 picResults.Cls Call AddAndIncrement picResults.Print picResults.Print "num1 = "; num1 picResults.Print "num2 = "; num2 End Sub Private Sub AddAndIncrement() 'Display numbers and their sum picResults.Print "The sum of"; num1; "and"; num2; "is"; num1 + num2 num1 = num1 + 1 num2 = num2 + 1 End Sub