Chapter 6 Sub Procedures And Functions Chapter 6
Chapter 6 Sub Procedures And Functions Chapter 6, Slide 1 Starting Out with Visual Basic 3 rd Edition
Chapter 6 Introduction A procedure is a collection of statements that performs a task. Chapter 6, Slide 2 Starting Out with Visual Basic 3 rd Edition
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 Is. Numeric § A method is a procedure declared in a class Chapter 6, Slide 3 Starting Out with Visual Basic 3 rd Edition
Section 6. 1 Sub 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 Chapter 6, Slide 4 Starting Out with Visual Basic 3 rd Edition
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 Chapter 6, Slide 5 Starting Out with Visual Basic 3 rd Edition
Sample Sub Procedure, Tutorial 6 -1 Private Sub btn. Go_Click(By. Val sender As System. Object, _ By. Val e As System. Event. Args) Handles btn. Go. Click ' This procedure calls the Display. Message procedure. lst. Output. Items. Add("Hello from btn. Go_Click procedure. ") lst. Output. Items. Add("Calling the Display. Message " & _ "procedure. ") Display. Message() lst. Output. Items. Add("Now I am back in the btn. Go_Click procedure. ") End Sub Returns to btn. Go_Click Calls Display. Message procedure Sub Display. Message() 'A Sub procedure that displays a message. lst. Output. Items. Add("") lst. Output. Items. Add("Hello from Display. Message. ") lst. Output. Items. Add("") End Sub Chapter 6, Slide 6 Starting Out with Visual Basic 3 rd Edition
Declaring a Sub Procedure [Access. Specifier] Sub Procedure. Name ([Parameter. List]) [Statement(s)] End Sub § Access. Specifier is optional and establishes accessibility to the program § Sub and End are keywords § Procedure. Name used to refer to procedure • Use Pascal casing, capitalize 1 st character of the name and each new word in the name § Parameter. List is a list of variables or values being passed to the sub procedure Chapter 6, Slide 7 Starting Out with Visual Basic 3 rd Edition
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 These will be discussed in later chapters § Access specifiers won’t be used for now § Practice writing procedures in Tutorial 6 -2 Chapter 6, Slide 8 Starting Out with Visual Basic 3 rd Edition
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 Variable. Name As Data. Type • Scope is only within the procedure • But variable exists for lifetime of procedure Chapter 6, Slide 9 Starting Out with Visual Basic 3 rd Edition
Section 6. 2 Passing Arguments to a Sub Procedure When calling a procedure, you can pass it values known as arguments Chapter 6, Slide 10 Starting Out with Visual Basic 3 rd Edition
Arguments § Argument – a value passed to a procedure § We’ve already done this with functions • Value = CInt(txt. Input. Text) • Calls CInt function and passes txt. Input. Text § A Sub must be declared so it accepts an argument Chapter 6, Slide 11 Starting Out with Visual Basic 3 rd Edition
Passing Arguments By Value Display. Value(5) ‘calls Display. Value procedure Sub Display. Value(By. Val number As Integer) ' This procedure displays a value in a message box. Message. Box. Show(number. To. String) 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 § Display. Value then executes § Tutorial 6 -3 demonstrates passing arguments Chapter 6, Slide 12 Starting Out with Visual Basic 3 rd Edition
Passing Multiple Arguments Show. Sum(5, 10) ‘calls Show. Sum procedure Sub Show. Sum(By. Val num 1 As Integer, By. Val num 2 As Integer) ' This procedure accepts two arguments, and prints ' their sum on the form. Dim sum As Integer sum = num 1 + num 2 Message. Box. Show("The sum is " & sum. To. String) End Sub § Multiple arguments separated by commas § Value of first argument is copied to first § Second to second, etc. Chapter 6, Slide 13 Starting Out with Visual Basic 3 rd Edition
Passing Arguments By. Val or By. Ref § Arguments are usually passed By. Val • • 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 won’t “see” the changes § Arguments can also be passed By. Ref • Procedure points to (references) argument’s original storage location • Any changes are made to the original value • Calling procedure “sees” the changes § Tutorial 6 -4 demonstrates this difference Chapter 6, Slide 14 Starting Out with Visual Basic 3 rd Edition
Section 6. 3 Function Procedures A Function Procedure Returns a Value to the Part of the Program That Called the Function Procedure Chapter 6, Slide 15 Starting Out with Visual Basic 3 rd Edition
Declaring a Function Procedure [Access. Specifier] Function. Name ([Parameter. List]) _ As Data. Type [Statements] End Function § New keyword Function § Also new is As Data. Type which states the data type of the value to be returned § Return value is specified in a Return expression Chapter 6, Slide 16 Starting Out with Visual Basic 3 rd Edition
Function Call Example total = Sum(value 1, value 2) Function Sum(By. Val num 1 As Single, By. Val num 2 As Single) _ As Single Dim result As Single result = num 1 + num 2 Return result End Function § value 1 and value 2 must be data type Single § Total must be declared as data type Single § Tutorial 6 -5 demonstrates function use Chapter 6, Slide 17 Starting Out with Visual Basic 3 rd Edition
Returning Nonnumeric Values Function Full. Name(By. Val first As String, By. Val last As String)_ As String Dim name As String name = last & ", " & first Return name End Function Is. Valid(num As Integer) As Boolean Dim status As Boolean If num >= 0 And num <= 100 Then status = True Else status = False End If Return status End Function Chapter 6, Slide 18 Starting Out with Visual Basic 3 rd Edition
- Slides: 18