256 Part II Programming Fundamentals
functions, however, Sub procedures don’t return values associated with their particular
Sub procedure names. Sub procedures are typically used to receive or process input,
display output, or set properties.
Function procedures and Sub procedures can be defined in a form’s program code, but for
many users, creating procedures in a module is more useful because then the procedures
have scope throughout the entire project. This is especially true for procedures that might
be called general-purpose procedures—blocks of code that are flexible and useful enough to
serve in a variety of programming contexts.
For example, imagine a program that has three mechanisms for printing a bitmap on
different forms: a menu command named Print, a Print toolbar button, and a drag-
and-drop printer icon. You could place the same printing statements in each of the three
event procedures, or you could handle printing requests from all three sources by using one
procedure in a module.
Advantages of General-Purpose Procedures
General-purpose procedures provide the following benefits:
n They enable you to associate a frequently used group of program statements
with a familiar name.
n They eliminate repeated lines. You can define a procedure once and have your
program execute it any number of times.
n They make programs easier to read. A program divided into a collection of
small parts is easier to take apart and understand than a program made up of
one large part.
n They simplify program development. Programs separated into logical units
are easier to design, write, and debug. Plus, if you’re writing a program in
a group setting, you can exchange procedures and modules instead of entire
programs.
n They can be reused in other projects and solutions. You can easily incorporate
standard-module procedures into other programming projects.
n They extend the Visual Basic language. Procedures often can perform tasks
that can’t be accomplished by individual Visual Basic keywords or Microsoft
.NET Framework methods.
Writing Function Procedures
A Function procedure is a group of statements located between a Function statement
and an End Function statement. The statements in the function do the meaningful work—
typically processing text, handling input, or calculating a numeric value. You execute, or call,