Procedures and Parameters

The learning objectives for this lab are:

  1. To understand the implementation of general procedures in VB6 as encapsulated functionality.
  2. To understand the difference between actual and formal parameters.
  3. To gain experience and comfort with the idea that the procedure provides a translation or mapping from the actual parameters (calling context) and the formal parameters (procedure context).
  4. To understand the distinction between general procedures and event procedures
  5. To gain experience and understanding with the role of typing variables in procedure definition and parameter passing.

In this lab we have two procedures. One provides concatenation functionality, while the other provides arithmetic addition functionality.

Program the application as directed by your TA.

After programming and debugging the project. Perform the following tests.

  1. Enter any whole number into both input boxes and test the both functions. What output did you get? Why?
  2. Enter some text into both input boxes and try both functions, what output do you get? Why?
  3. Try writing new procedures. For example write a procedure similar to the AddTwoIntegers procedure that multiplies two integers and displays the output. Use a new command button to fire your new procedure.
  4. The ConcatenateTwoStrings procedure places the string in the First box in front of the string in the Second input box. Create a new procedure that places the string from the Second box in front of the string from the first box. Use a new command button to call your new procedure. You can implement this by defining an entirely new procedure. You can also implement this by having a new procedure call the ConcatenateTwoStrings procedure. Can you figure out how?

Key terms

 

Option Explicit

' This is a general procedure that concatenates two strings

' Note that the operation is applied to formal parameters

Private Sub ConcatenateTwoStrings(strOut As String, strFirst As String, strSecond As String)

strOut = strFirst & strSecond

End Sub

 

' This is a general procedure that adds two integers together

' Note that the operation is applied to formal parameters

Private Sub AddTwoIntegers(intOut As Integer, intFirst As Integer, intSecond As Integer)

intOut = intFirst + intSecond

End Sub

' This is an event procedure that declares actual parameters, sets the values of two input parameters

' to the contents of text boxes on the form, then passes these parameters to a general procedure.

' The output parameter of the general procedure is then used to set the caption property of a label

' so we can read it.

Private Sub cmdAdd_Click()

Dim intA As Integer

Dim intB As Integer

Dim intC As Integer

intA = txtFirst.Text

intB = txtSecond.Text

Call AddTwoIntegers(intC, intA, intB)

lblAdd.Caption = intC

End Sub

Private Sub cmdConcatenate_Click()

Dim strA As String

Dim strB As String

Dim strC As String

strA = txtFirst.Text

strB = txtSecond.Text

Call ConcatenateTwoStrings(strC, strA, strB)

lblConcatenate.Caption = strC

End Sub

Private Sub Form_Load()

' Format the labels when the form loads

lblConcatenate.Caption = ""

lblConcatenate.BackColor = vbRed

lblAdd.Caption = ""

lblAdd.BackColor = vbWhite

' Set the text in the input boxes to empty strings so we

' don't anything there when we start the program

txtFirst.Text = ""

txtSecond.Text = ""

End Sub