Upload
ashlie-eaton
View
216
Download
0
Embed Size (px)
DESCRIPTION
Procedures Global Variables: ‘Create 2 global variables outside of any procedure, can be used anywhere Create variables 1, 2 Button click event procedure: Add() Multiply() End of event procedure Procedure Add(): ‘1 local variable, only used ‘inside this procedure Create variable 3 ‘use global variable 1, and 3 Take the user input and store in variable 1 and 2 Add variable 1 to variable 2 and store the result to variable 3 End of procedure Add Procedure Multiply() … End of Multiply procedure Can be changed to the following: Button click event procedure: Create variables 1, 2 Take the user input and store in variable 1 and 2 ‘Tell the procedure what to add Add(variable1 and variable2) ‘Tell the procedure what to multiply Multiply(variable1 and variable2) End of event procedure Procedure Add(variable1 and variable2): Create variable 3 Add variable 1 to variable 2 and store in variable 3 End of Add procedure Procedure Multiply(variable1 and variable2): Create variable 3 Multiply variable 1 to variable 2 and store in variable 3 End of Multiply procedure
Citation preview
6b – Sub Procedure With Parameters
Lingma AchesonDepartment of Computer and Information Science, IUPUI
CSCI N331 VB .NET Programming
Procedure Revisit• Add an item into the list box:
‘Contents inside the () defines what to add to the listbox lstOutput.Items.Add(“*************”) ‘Contents inside the () defines what to show in the
messageboxMessageBox.Show(“The second input is 0. Unable to perform the division.”)
• We can pass data to the procedure and give some information for the procedure to use.
2
ProceduresGlobal Variables:‘Create 2 global variables outside of any procedure, can be used anywhereCreate variables 1, 2 Button click event procedure: Add() Multiply()End of event procedureProcedure Add(): ‘1 local variable, only used ‘inside this procedure Create variable 3 ‘use global variable 1 , and 3 Take the user input and store in variable 1 and 2 Add variable 1 to variable 2 and store the result to variable 3End of procedure AddProcedure Multiply() …End of Multiply procedure
Can be changed to the following:Button click event procedure: Create variables 1, 2 Take the user input and store in variable 1 and 2 ‘Tell the procedure what to add Add(variable1 and variable2) ‘Tell the procedure what to multiply Multiply(variable1 and variable2)End of event procedureProcedure Add(variable1 and variable2): Create variable 3 Add variable 1 to variable 2 and store in variable 3End of Add procedureProcedure Multiply(variable1 and variable2): Create variable 3 Multiply variable 1 to variable 2 and store in variable 3End of Multiply procedure
Parameters• Sub procedure that takes parameters
– Purpose: Pass data into the Sub for the Sub to use– Example: Sub call:
Add(2.5, 3.1) Sub Definition:
Private Sub Add(ByVal dblValue1 As Double, ByVal
dblValue2 As Double) Dim dblResult as Double
dblResult = dblValue1 + dblValue2 End Sub
4
Parameters• Sub procedure that takes parameters
– More than one value can be passed into the Sub. If more than one, use comma to separate the values.
– The number of values passed in must match the number of parameters in the definition.
– The types of values passed in must match the types of parameters in the definition.
5
Parameters• Sub procedure that takes parameters
– The parameter name defined in the procedure definition doesn’t have to be the same as the actual variable name passed into the procedure. E.g.: We can call add twice with different variables. Sub call:
Add(2.5, 3.1) Add(dblInput1, dblInput2) Add(dblInput3, dblInput4)
Sub Definition:Private Sub Add(ByVal dblValue1 As Double, ByVal dblValue2 As
Double) Dim dblResult as Double
dblResult = dblValue1 + dblValue2 End Sub– Sub procedures with parameters are very useful if we want
to perform the same task again with different values.6
Sub Procedures– E.g.
Private Sub btnCompute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
Dim intFirst As IntegerDim intSecond As IntegerintFirst = CInt(nudFirst.Value)
intSecond = CInt(nudSecond.Value)‘intFirst and intSecond are local variables, thus they cannot
be used ‘inside the Add() procedure. Their values must be passed into the Add().
Add(intFirst, intSecond) ‘Sub call End Sub
‘Sub definition, takes two parameters, both are integersPrivate Sub Add(ByVal intOne As Integer, ByVal intTwo As Integer)
Dim intResult As Integer = 0intResult= intOne + intTwotxtAddResult.Text = CStr(intResult)
End Sub
7