Datasheet

Chapter 1: A Quick Introduction to Programming
20
Modularization, Black Boxes,
Procedures, and Subprocedures
Modularization is the process of organizing your code into modules, which you can also think of as build-
ing blocks. You can apply the principles of modularity to create your own personal set of programming
building blocks, which you can then use to build programs that are more powerful, more reliable, easier
to debug, and easier for you and your fellow programmers to maintain and reuse. When you take your
code and divide it into modules, your ultimate goal is to create what are known as black boxes. A black
box is any kind of device that has a simple, well-defined interface and that performs some discrete, well-
defined function. A black box is so called because you don’t need to see what’s going on inside it. All you
need to know is what it does, what its inputs are, and (sometimes) what its outputs are.
A wristwatch is a good example of a black box. It has inputs (buttons) and outputs (time) and does
a simple function well without you worrying about how the innards of the watch work in order to
be able to tell the time. The most basic kind of black box programmers use to achieve modularity
is the procedure. A procedure is a set of code that (ideally) performs a single function. Good examples
of procedures are:
Code that adds two numbers together
Code that processes a string input
Code that handles saving to a file
Bad examples include:
Code that takes an input, processes it, and also handles saving to a file
Code that handles file access and database access
You’ve been using procedures throughout this chapter, but they have been procedures that VBScript
provides for you. Some of these procedures require input, some don’t. Some of these procedures return
a value, some don’t. But all of the procedures you’ve used so far (
MsgBox() , InputBox() , and so on)
are black boxes. They perform one single well-defined function, and they perform it without you having
to worry about how they perform their respective functions. In just a moment, you’ll see how to extend
the VBScript language by writing your own procedures.
Before you begin though, it’s time to get some of the terminology cleared up. Procedure is a generic term
that describes either a function or a subprocedure. This chapter touched on some of this confusing termi-
nology earlier, but a function is simply a procedure that returns a value.
Len() is a function. You pass it
some text, and it returns the number of characters in the string (or the number of bytes required to store
a variable) back to you. Functions do not always require input, but they often do.
A subprocedure is a procedure that does not return a value. You’ve been using
MsgBox() as a subproce-
dure. You pass it some text, and it displays a message on the screen comprising of that text. It does not
return any kind of value to your code. All you need to know is that it did what you asked it to do. Just
like functions, procedure may or may not require input.
c01.indd 20c01.indd 20 8/27/07 7:45:24 PM8/27/07 7:45:24 PM