The one thing you should have learned about programming in Visual Basic so far is that an application is made up of small, self-contained segments. The code you write isn’t a monolithic listing; it’s made up of small segments called procedures, and you work on one procedure at a time. The two types of procedures supported by Visual Basic are the topics we’ll explore in this chapter: subroutines and functions — the building blocks of your applications. We’ll discuss them in detail: how to call them with arguments and how to retrieve the results returned by the functions. You’ll learn how to use the built-in functions that come with the language, as well as how to write your own subroutines and functions.
The statements that make up the core of the language are actually very few. The flexibility of any programming language is based on its capacity to alter the sequence in which the statements are executed through a set of so-called flow-control statements. These are the statements that literally make decisions and react differently depending on the data, user actions, or external conditions. Among other topics, in this chapter you’ll learn how to do the following:
Use Visual Basic’s flow-control statements – Visual Basic provides several statements for controlling the sequence in which statements are executed: decision statements, which change the course of execution based on the outcome of a comparison, and loop statements, which repeat a number of statements while a condition is true or false.
Write subroutines and functions – To manage large applications, we break our code into small, manageable units. These units of code are the subroutines and functions. Subroutines perform actions and don’t return any values. Functions, on the other hand, perform calculations and return values. Most of the language’s built-in functionality is in the form of functions.
Pass arguments to subroutines and functions – Procedures and functions communicate with one another via arguments, which are listed in a pair of parentheses following the procedure’s name. Each argument has a name and a type. When you call the procedure, you must supply values for each argument and the types of the values should match the types listed in the procedure’s definition.
What makes programming languages so flexible and capable of handling every situation and programming challenge with a relatively small set of commands is their capability to examine external or internal conditions and act accordingly. Programs aren’t monolithic sets of commands that carry out the same calculations every time they are executed; this is what calculators (and extremely simple programs) do. Instead, they adjust their behavior depending on the data supplied; on external conditions, such as a mouse click or the existence of a peripheral; even on abnormal conditions generated by the program itself.
In effect, the statements discussed in the first half of this chapter are what programming is all about. Without the capability to control the flow of the program, computers would just be bulky calculators. You have seen how to use the If statement to alter the flow of execution in previous chapters, and I assume you’re somewhat familiar with these kinds of statements. In this section, you’ll find a formal discussion of flow-control statements. These statements are grouped into two major categories: decision statements and looping statements.
Applications need a mechanism to test conditions and take a different course of action depending on the outcome of the test. Visual Basic provides three such decision, or conditional, statements:
- If. . .Then
- If. . .Then. . .Else
- Select Case
Loop statements allow you to execute one or more lines of code repetitively. Many tasks consist of operations that must be repeated over and over again, and loop statements are an important part of any programming language. Visual Basic supports the following loop statements:
- For. . .Next
- Do. . .Loop
- While. . .End While