Using General Sub
functions in Applications
• Using General Sub procedures can
help divide a complex application
into more manageable units of code.
This helps meet the above stated
goals of readability and reusability.
• Most applications have tasks not
related to objects that require
some code to perform these tasks.
Such tasks are usually coded in a
general sub procedure, essentially
the same as a routine in other
• Breaking your programs into as many
small but logical sections as possible.
the smaller routines make your
programming and subsequent
• In many traditional programming
languages such as COBOL and FORTRAW,
a program is like a long book without
chapters. The code goes on and on
and the program’s length is exceeded
only by the boredom programmer’s
face trying to wade through the
code hunting down errors.
A Visual Basic program consists of:
• A form with controls that act as the
program’s background and user
• A general-purpose procedure named,
found in the Code Window’s object
• Event procedures that tie the
controls together and add specific
direction and calculations to the
• A CONSTANT.TXT file that provides
named constants used by your code
Kinds of Procedures
executes as events occur
Procedure is really
not an executable
procedure in the way
that event procedures
Defining a Sub Procedure/Routines
• A sub procedure or a subroutine
is a procedure that executes the
lines of code within its block but
doesn’t return a value. The syntax
for a simple sub is as follows:
Sub GenlSubProc (Arguments)
The definition header names the Sub
procedure and defines any argument
passed to the procedure.
Arguments are a comma-delimited list of
variables passed to and/or from the
If there are arguments, they must be
declared and typed in the definition
header in this form:
Var1as Type1, Var2 as Type2…
A subroutine always begin with the Sub
statement and always ends with the End
A subroutine may or may not be an event
*Example of a code that contains an
• Event procedures are specific subroutines
tied directly to control events.
• When you write a section of code that
your application will have to execute more
than once, that section of code is a great
candidate for a general-purpose, non-
Sub cmdExit_Click ()
BENEFITS FROM USING SUB
Codes will be reusable
Program code will be
simpler to maintain
• You can add a sub to your
project in two ways:
• By writing the code directly
into the General Declarations
section of a form or module;
• By using the Tools menu’s Add
ENABLING THE ADD PROCEDURE
For the Add procedure menu item to be
enabled, you must be in the Code window
view of the form or module into which
you want to add the procedure.
ADD A SUB TO YOUR PROJECT WITH THE ADD
1. From the Tools menu, choose Add
procedure to display the Add procedure
2. Enter the Sub name.
3. Click OK to add the sub’s code block to
the form or module.
TYPING THE SUBROUTINE IN
THE CODE WINDOW
1. Go to the Code
2.Type Sub GenAverage.
3.Press Enter key.
In selecting the Insert Procedure Menu
item, note another option for your
procedure is Scope. You have the option of
Public or Private.
If a module procedure is public, it can be
called from any other procedure in other
If a module procedure is Private, it can
only be called from the module it is
NOTE: Scope only applies to
procedures in modules.
*All event procedures and general
procedures in a form are Private.
CALLING A SUB PROCEDURE
• Method 1:
Call GenlSubProc (Arguments) (if
there are no Arguments, do not type
• Method 2:
CREATING A PROCEDURE
1. Create a project.
2. Add the following controls in
your form with the
Object Properties Values
Label 1 Caption US Dollar
Label 2 Caption Peso
Label 3 Caption Peso Value
3. Add a procedure name
USPesoConvert by using the
Add Procedure in the tools
4. Type the following code
for each procedure as
Private Sub cmdConvert_Click
Private Sub Form_Load()
Text 1 = “ “
Text 2 = “ “
Text 3 = “ “
Public Sub USPesoConvert ()
Text 3 = Text1 & Text2
To call dollar exchange routine, we
Private Sub cmdConvert_Click ()
Private Sub cmdConvert_Click ()
5. Press F5 to execute the
PASSING ARGUMENTS TO SUB
You can enhance the power and
versatility of subs and functions
by using arguments.
ARGUMENTS – also referred to as a
PARAMETER- a variable that acts as a
placeholder for a value that
you’ll pass into the sub or
Using arguments greatly increases
the reusability of your code.
THE ADVANTAGES OF THE LATTER
METHOD IS TWOFOLD:
One call satisfies many needs
throughout your code.
If you need to enhance this
functionality, you don’t have
to go through your code and
make enhancements line by line,
you simply go back to the
function and make the changes
within the function’s code
USING GENERAL FUNCTION
PROCEDURES IN APPLICATIONS
Function procedures work
a lot like subroutine; you
can call them from
elsewhere in the
procedures return values.
• Private/Public are the optional Visual
Basic keywords that define the scope
of the function.
• Function is the Visual Basic keyword
that denotes the procedure is a
• FunctionName is the name that you
assign to your function.
• As is the Visual Basic keyword that
denotes the procedure is a function.
• DataType is the data type of the value
that the function will return.
• ReturnValue is the value that you
pass back from the function by
assigning it top the function’s name
(this is very important).
• End Function is the Visual Basic
keywords that denote the end of a
• You add a function to your project
by using the same two methods that
you use to add a subroutine.
However, he advised that you have
to manually add a little code when
you add a function to your code by
using the Add Procedure dialog.
• A function procedure always begins
with the Function statement and
ends with the End function
• A function is the same in every way
as a subroutine except that the
function returns a value.
• All you do to call a function is to
use the function procedure’s name
inside an expression or statement.
• If you ever need to exit a function
procedure before the function’s
normal termination, use the Exit
LOCATING FUNCTION PROCEDURES
• Can be located in forms
• They are created using
exactly the same process
described for Sub
procedures. The only
difference is you use the