Sub Procedures - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

Sub Procedures

Description:

A Function procedure is a collection of statements that ... to refer to procedure ... variable with scope only within the sub procedure where declared ... – PowerPoint PPT presentation

Number of Views:60
Avg rating:3.0/5.0
Slides: 22
Provided by: russel60
Category:

less

Transcript and Presenter's Notes

Title: Sub Procedures


1
Chapter 6
  • Sub Procedures
  • And Functions

2
Chapter 6Introduction
  • A procedure is a collection of statements that
    performs a task.

3
Chapter 6 Topics
  • A Sub procedure is a collection of statements
    that performs a task
  • An abbreviation of the older term subroutine
  • Event procedures are Sub procedures
  • A Function procedure is a collection of
    statements that performs a task and returns a
    value to the VB statement that executed it
  • Function procedures work like intrinsic
    functions, such as Val and IsNumeric
  • A method is a procedure declared in a class

4
Section 6.1Sub Procedures
  • You Can Write Your Own General Purpose Sub
    Procedures That Perform Specific Tasks
  • General Purpose Sub Procedures Are Not Triggered
    by Events but Called From Statements in Other
    Procedures

5
Sub Procedure Uses
  • May handle events such as a click event
  • Also used to simplify a program by
  • Breaking it into small, manageable pieces or
  • Performing a task that is needed repeatedly
  • Sub procedures help to modularize code
  • Divides a program into a set of logical tasks

6
Sample Sub Procedure, Tutorial 6-1
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("He
llo from btnGo_Click procedure.") lstOutput.Items
.Add("Calling the DisplayMessage "
_ "procedure.") DisplayMessage() lstOutput.Ite
ms.Add("Now I am back in the btnGo_Click procedu
re.") 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
7
Declaring a Sub Procedure
AccessSpecifier Sub ProcedureName
(ParameterList) Statement(s) End Sub
  • AccessSpecifier is optional and establishes
    accessibility to the program
  • Sub and End are keywords
  • ProcedureName used to refer to procedure
  • Use Pascal casing, capitalize 1st character of
    the name and each new word in the name
  • ParameterList is a list of variables or values
    being passed to the sub procedure

8
More on Access Specifier
  • Private allows use only from that form or class
  • Public allows use from other forms or classes
  • If not specified, default is Public
  • Additional access specifiers
  • Protected
  • Friend
  • Protected Friend
  • These will be discussed in later chapters
  • Access specifiers wont be used for now
  • Practice writing procedures in Tutorial 6-2

9
Procedures and Static Variables
  • Variables needed only in a Sub procedure, should
    be declared within the Sub procedure
  • Creates a local variable with scope only within
    the sub procedure where declared
  • Local variable values are not saved from one sub
    procedure call to the next
  • To save value between procedure calls, use Static
    keyword to create a static local variable
  • Static VariableName As DataType
  • Scope is only within the procedure
  • But variable exists for lifetime of procedure

10
Section 6.2Passing Arguments to a Sub Procedure
  • When calling a procedure, you can pass it values
    known as arguments

11
Arguments
  • Argument a value passed to a procedure
  • Weve already done this with functions
  • Value CInt(txtInput.Text)
  • Calls CInt function and passes txtInput.Text
  • A Sub must be declared so it accepts an argument

12
Passing Arguments By Value
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
  • Number declared as an integer argument
  • Storage location number created
  • A value, 5 in this case, must be supplied and is
    copied into the storage location for number
  • DisplayValue then executes
  • Tutorial 6-3 demonstrates passing arguments

13
Passing 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
  • Multiple arguments separated by commas
  • Value of first argument is copied to first
  • Second to second, etc.

14
Passing Arguments ByVal or ByRef
  • Arguments are usually passed ByVal
  • New storage location created for procedure
  • Storage location gets a copy of the value
  • Any changes in value are made to the copy
  • Calling procedure wont see the changes
  • Arguments can also be passed ByRef
  • Procedure points to (references) arguments
    original storage location
  • Any changes are made to the original value
  • Calling procedure sees the changes
  • Tutorial 6-4 demonstrates this difference

15
Section 6.3Function Procedures
  • A Function Procedure Returns a Value to the Part
    of the Program That Called the Function Procedure

16
Declaring a Function Procedure
AccessSpecifier Function FunctionName
(ParameterList) _ As DataType Statements E
nd Function
  • New keyword Function
  • Also new is As DataType which states the data
    type of the value to be returned
  • Return value is specified in a Return expression

17
Function Call Example
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
  • value1 and value2 must be data type Single
  • Total must be declared as data type Single
  • Tutorial 6-5 demonstrates function use

18
Returning Nonnumeric Values
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(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
19
Section 6.4More About Debugging
  • Step Into
  • Step Over
  • Step Out

20
Debugging Involving Procedures
  • Step Into - continue to debug by single-stepping
    through a procedure
  • Step Over - run procedure without
    single-stepping, continue single-step after the
    call
  • Step Out - end single-stepping in procedure,
    continue single-step after the call
  • Tutorial 6-6 provides examples

21
Section 6.5Building the Bagel and Coffee Price
Calculator Application
  • Use Sub procedures and functions to calculate the
    total of a customer order.
Write a Comment
User Comments (0)
About PowerShow.com