Download presentation
Presentation is loading. Please wait.
1
Chapter 7: Using Functions, Subs, and Modules
Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
2
Plan for Revising Vintage Videos Application
Need a way of adding videos to system Need a better way of managing membership list Need capability to add late fees to customer’s bill Need a system to print alphabetical list of members or videos Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
3
Design for Expanded Vintage Videos Project
Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
4
Membership Management Form
Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
5
Modified Videos Form Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
6
Using General Procedures
Event procedures are associated with a particular event and are not usually available to other forms. General procedures are used for specific tasks that are not associated with an event. General procedures are defined in the General Object of a form and then invoked elsewhere in the project. Two types of general procedures: subs (subroutines) functions Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
7
Relationship Between General and Event Procedures
Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
8
Subs and Functions A sub is a unit of code that performs a specific task but returns no value. A function is similar to the built in functions in that arguments are passed to it and processed to compute a single value returned by its name. Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
9
Primary Purposes of Subs and Functions
Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
10
Working with General Procedures
General procedures must be created and then invoked. Invoking a function: variable = functionname(arg1, arg2, …, argn) Invoking a sub: subname arg1, arg2, …, argn Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
11
Creating Subs and Functions
To create a sub or function you can Use the Tools|Add Procedure menu command and select the type of procedure to add or simply type the word Sub or Function and press Enter after any existing event or general procedure. In either case, then add the parameters. Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
12
Creating a Function The general form of the function definition statement is: Function FuncName(parameter1 as type, parameter2 as type, …) as type For example Function intFindMax(intNum1 as Integer, intNum2 as Integer) as Integer An important rule is that the name of the function must be assigned a value in the function. Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
13
Creating a Sub The general form of the sub definition statement is:
Sub SubName (parameter1 as type, parameter2 as type, …) Note that the sub name is not assigned a data type Example Sub Reverse(curFirst as Currency, curSecond as Currency) Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
14
Relationship Between Sub Definition Statement and Statement Invoking the Sub
Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
15
Relationship Between Function Definition Statement and the Statement Invoking the Function
Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
16
Matching Arguments and Parameters
For both sub and functions, the number and type of arguments in the invoking statement must match the number and type of parameters in the procedure definition statement. In both the argument and parameter list, fixed-size arrays are referenced by the name of the array followed by parentheses. Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
17
Function to Compute Income Taxes
Public Function curComputeTaxes(intNumDep As Integer, _ curGrossIncome As Currency) as Currency Dim curTaxIncome As Currency curTaxIncome = curGrossIncome intNumDep * 2650 Select Case curTaxIncome Case Is <= 24650 curComputeTaxes = 0.15 * curTaxIncome Case Is <= 59750 curComputeTaxes = * (curTaxIncome ) Case Is <= curComputeTaxes = * (curTaxIncome ) Case Is < curComputeTaxes = * (curTaxIncome ) Case Else curComputeTaxes = * (curTaxIncome ) End Select End Function Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
18
Sub to Reverse Two Values
Sub Reverse(curFirst as Currency, curSecond as _ Currency) Dim curTemp as Currency curTemp = curFirst curFirst = curSecond curSecond = curTemp End sub Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
19
Sub to Sort Arrays Public Sub Sort(curFirstList() As Currency, _
curSecondList()As Currency, intNumList As Integer) Dim blnNoReversal As Boolean, intCounter As Integer blnNoReversal = False Do Until blnNoReversal blnNoReversal = True For intCounter = 0 To intNumList - 2 If curFirstList(intCounter ) > _ curFirstList(intCounter + 1) Then Reverse curFirstList(intCounter ), _ FirstList(intCounter + 1) StrReverse curSecondList(intCounter ), _ curSecondList(intCounter +1) End If Next Loop End Sub Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
20
Global Declarations and the Code Module
In a global declaration, the scope of global variables, as compared to form-level variables or procedure-level variables, includes all parts of the project. The Code Module is the section of pure code that is known to all parts of the project. Use Public statement to declare variables Public varName1 as type, varName2 as type, ... Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
21
Scope of Global Variables
Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
22
Use of String Functions
Len(string) --returns number of characters in string. Left(string, N) or Right(string, N) --returns the leftmost or rightmost N characters in a string. Mid(String,P,N) --returns N characters in a string starting at Pth character. LTrim(string) or RTrim(string) --trims blank characters from left (right) end of string. Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
23
Passing by Value in Subs
If there is a two-way communication between arguments and parameters, then you have passing by reference. If there is a one-way communication between arguments and parameters, then you have passing by value. It is possible to force passing by value by adding the keyword ByVal prior to a variable in the procedure definition statement. Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.