Professional Documents
Culture Documents
Programming
1
Chapter
General Procedures
5
A procedure is a collection of
statements that performs a task
2
Introduction
3
Procedures
5
Sample Procedure
Private Sub btnGo_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnGo.Click
' This procedure calls the DisplayMessage procedure.
lstOutput.Items.Add("Hello from btnGo_Click procedure.")
lstOutput.Items.Add("Calling the DisplayMessage " & _
"procedure.")
DisplayMessage()
lstOutput.Items.Add("Now I am back ” _
& “in the btnGo_Click procedure.")
End Sub
Calls
Returns to DisplayMessage
btnGo_Click procedure
Sub DisplayMessage()
'A Sub procedure that displays a message.
lstOutput.Items.Add("")
lstOutput.Items.Add("Hello from DisplayMessage.")
lstOutput.Items.Add("")
End Sub 6
Declaring a Procedure
[AccessSpecifier] Sub ProcedureName ([ParameterList])
[Statements]
End Sub
7
More on the Access Specifier
• Private allows use only from that form
• Public allows use from other forms
• If not specified, default is Public
• There are other access specifiers such as:
• Protected
• Friend
• Protected Friend
• Access specifiers won’t be used for now
8
Procedures
• Variables needed only in a procedure,
should be declared within that
procedure
• Creates a local variable with scope only
within the procedure where declared
• Local variable values are not saved from
one procedure call to the next
9
Passing Arguments to a
Procedure
10
Arguments
• Argument – a value passed to a procedure
• We’ve already done this with functions
• Value = CInt(txtInput.Text)
• Calls the CInt function and passes txtInput.Text as an
argument
• A procedure must be declared with a parameter list
in order to accept an argument
11
Passing Arguments By Value
DisplayValue(5) ‘calls DisplayValue procedure
12
Passing Multiple Arguments
ShowSum(intValue1, intValue2) ‘calls ShowSum procedure
14
ByVal or ByRef Argument Example
17
Function Call Example
sngTotal = Sum(sngValue1, sngValue2)