Procedures+and+Parameters

__ ICS3CU –Procedures and Parameters __ In VB, we have seen how you can have a “click” procedure or a “key_down” procedure. These are event procedures – they occur upon an ** __event__ ** in Visual Basic. However, we also have the ability to create our own “general procedures”. These are just procedures that simplify programs by breaking them up into smaller, more manageable blocks of code. For example, what if you have 3 different buttons, but you want pretty much the same thing to happen each time you click any of them? This is where it gets very handy to write your own procedures. With a procedure, you can write a chunk of code that will be executed whenever you want it to be executed. You can almost think of a procedure like cooking food and freezing it. Let’s say I like chili. I can make an enormous batch of chili and freeze a whole bunch of it in individual servings. So when I’m hungry, rather than going to the kitchen and having to make chili all over again from scratch, I can just pull one out of the freezer. Let’s look at an example in some code: __ PROCEDURES WITHOUT PARAMETERS __ Let’s look at a program where there are 3 buttons. If you click any one of the three, the same thing happens – a click counter increases and a message occurs in the box saying a button was clicked. Dim intClickCounter As Integer Private Sub Form_Load ‘ initialize a counter intClickCounter = 0 End Sub Private Sub cmdA_Click ‘ what happens when I click button A  GiveOutput End Sub Private Sub cmdB_Click ‘ what happens when I click button B  GiveOutput End Sub Private Sub cmdC_Click ‘ what happens when I click button C  GiveOutput End Sub Private Sub GiveOutput ‘ my GiveOutput procedure txtOutput.Text = "Someone Clicked a Button" intClickCounter = intClickCounter + 1 lblCount.Caption = intClickCounter If intClickCounter < 10 Then lblOutput.Caption = "Under 10 clicks." ElseIf intClickCounter < 20 Then lblOutput.Caption = "Under 20 clicks." ElseIf intClickCounter < 30 Then lblOutput.Caption = "Under 30 clicks." End If End Sub  __ Parameters __ At times, you will create a procedure that needs data in order to work. Data is “passed” to a procedure by “calling” the procedure and then including parentheses that pass in the values: Some definitions: § MyProcedure(ByVal strInput as String) o “ByVal” is indicating that a parameter is coming o “strInput” is the parameter o “as String” indicates that a String parameter is being used __ SAMPLE CODE – Procedure with One Parameter
 * i.e. ThisProcedure(strInput) ‘ the procedure is called ThisProcedure and the data ‘ is a string variable “strInput” ||

__ Dim intClickCounter As Integer Private Sub Form_Load intClickCounter = 0 End Sub Private Sub cmdA_Click GiveOutput("A") End Sub Private Sub cmdB_Click GiveOutput("B") End Sub Private Sub cmdC_Click GiveOutput("C") End Sub Sub GiveOutput(ByVal strButton As String) intClickCounter = intClickCounter + 1 lblCount.Caption = intClickCounter If intClickCounter < 10 Then lblOutput.Caption = "Under 10 clicks." txtOutput.Text = "Someone Clicked Button " & strButton ElseIf intClickCounter < 20 Then lblOutput.Caption = "Under 20 clicks." txtOutput.Text = "I love button " & strButton ElseIf intClickCounter < 30 Then lblOutput.Caption = "Under 30 clicks." txtOutput.Text = "Button " & strButton & " Rocks" End If End Sub  What if you need more than one parameter? Let’s create a procedure that makes a sentence using three words: I would define the procedure like this: Sub LettersInOrder(ByVal strFirst as String, ByVal strSecond as String, ByVal strThird as String) lblOutput.Caption = strFirst & “ is first and “ strSecond & “ is the second and “ strThird “ is last.” End Sub Now, in the code, if you wanted to use the procedure, you could call the procedure like this: LettersInOrder(“M”, “N”, “O”) - in this case “strFirst” is “M”, “strSecond” is “N”, and “strThird” is “O” OUTPUT: M is first and N is the second and O is last. If I were to make a mistake and include my parameters out of order, the sentence would be messed up: LettersInOrder(“O”, “N”, “M”) - in this case “strFirst” is “O”, “strSecond” is “N”, and “strThird” is “M” OUTPUT: O is first and N is the second and M is last. So order DOES matter!