Title: Sub Procedures and Functions
1Chapter 6
- Sub Procedures and Functions
2Agenda
- Sub Procedures
- Passing Arguments
- Function Procedures
- Run through tutorials
3Definitions
- A Sub procedure is a collection of statements
that performs a task - Example Event procedures
- A Function procedure is a collection of
statements that performs a task and returns a
value to the VB statement that executed it - Example Val and IsNumeric
- A method is a procedure declared in a class
- Subs and Functions are methods
4Sub Procedures
- Help to modularize the code
- Divide it up into a set of logical tasks
- Simplify the program
- Break up the code into pieces
- Use for repeated tasks
- Click events
5Sample
- 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
Returns to btnGo_Click
Calls DisplayMessage procedure
Sub DisplayMessage() 'A Sub procedure that
displays a message. lstOutput.Items.Add("") lstO
utput.Items.Add("Hello from DisplayMessage.") lst
Output.Items.Add("") End Sub
6Declaring a Sub Procedure
- AccessSpecifier Sub ProcedureName
(ParameterList) - Statement(s)
- End Sub
- Access Specifier Optional, establishes the
accessibility of the sub - Public vs. Private
- Procedure Name used to call the sub
- Parameter List a list of variables or values
that are being passed into the sub
7Issues with Variables
- Local Variable within the scope of the
procedure in which it is declared - Not saved from one procedure call to the next
- Values are reset
- Static Local Variable saves value of variable
between procedure calls - Static VariableName As DataType
8Arguments
- Argument a value passed to a procedure
- Value CInt(txtInput.Text)
- The sub must be declared to accept an argument
- Example
DisplayValue(5) calls DisplayValue
procedure Sub DisplayValue(ByVal number As
Integer) ' This procedure displays a value in a
message box. MessageBox.Show(number.ToString) End
Sub
9Multiple Arguments
- ShowSum(5, 10) calls ShowSum procedure
- Sub ShowSum(ByVal num1 As Integer, ByVal num2 As
Integer) - ' This procedure accepts two arguments, and
prints - ' their sum on the form.
- Dim sum As Integer
- sum num1 num2
- MessageBox.Show("The sum is " sum.ToString)
- End Sub
10ByVal vs. ByRef
- ByVal
- New variable is created and assigned the value of
the passing argument - Changes are made to the copy
- ByRef
- Procedure points to the original variable
- Changes are made to the orginal
11Functions
AccessSpecifier Function FunctionName
(ParameterList) _ As DataType Statements E
nd Function
total Sum(value1, value2) Function Sum(ByVal
num1 As Single, ByVal num2 As Single) _ As
Single Dim result As Single result num1
num2 Return result End Function
12Functions
Function FullName(ByVal first As String, ByVal
last As String)_ As String Dim name As
String name last ", " first Return
name End Function
Function IsValid(ByVal num As Integer) As
Boolean Dim status As Boolean If num gt 0 And
num lt 100 Then status True Else status
False End If Return status End Function