Chapter 6. Programming in the Large

Methods

Table of Contents

1. Black Boxes
2. Defining Methods
3. Using Shared Variables
4. Parameters
5. Return Values
6. More About Parameters
7. Unit Testing
8. Method Miscellany

ONE WAY TO BREAK UP A COMPLEX PROGRAM into manageable pieces is by dividing it into separate methods. A method consists of the instructions for carrying out a certain task, grouped together and given a name. Elsewhere in the program, that name can be used as a stand-in for the whole set of instructions. As a computer executes a program, whenever it encounters a method name, it executes all the instructions necessary to carry out the task associated with that method.

Methods can be used over and over, at different places in the program. A method can even be used inside another method. This allows you to write simple methods and then use them to help write more complex methods, which can then be used in turn in other methods. In this way, very complex programs can be built up step-by-step, where each step in the construction is reasonably simple.

As mentioned in Chapter 4, methods in C# can be either static or non-static. This chapter covers static methods only. Non-static methods, which are used in true object-oriented programming, will be covered in a later chapter.