Sub Procedures and Functions - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

Sub Procedures and Functions

Description:

A Function procedure is a collection of statements that ... Divide it up into a set of logical tasks. Simplify the program. Break up ... the orginal. Functions ... – PowerPoint PPT presentation

Number of Views:37
Avg rating:3.0/5.0
Slides: 13
Provided by: Jos562
Category:

less

Transcript and Presenter's Notes

Title: Sub Procedures and Functions


1
Chapter 6
  • Sub Procedures and Functions

2
Agenda
  • Sub Procedures
  • Passing Arguments
  • Function Procedures
  • Run through tutorials

3
Definitions
  • 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

4
Sub 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

5
Sample
  • 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
6
Declaring 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

7
Issues 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

8
Arguments
  • 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
9
Multiple 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

10
ByVal 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

11
Functions
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
12
Functions
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
Write a Comment
User Comments (0)
About PowerShow.com