Section 7.3. Subroutines: Methods That Do Not Return a Value


7.3. Subroutines: Methods That Do Not Return a Value

The programs presented earlier in the book each contain at least one method declaration that calls FCL methods (such as Console.WriteLine) to accomplish the program's tasks. All of the methods that you have defined so far, such as DisplayMessage in class GradeBook, are known as subroutinesmethods that perform tasks but do not return a value. Functions are methods that do return a value to the calling method. An example of a function that you have seen is Console.ReadLine, which returns to the caller the data entered by the user at the keyboard. We show how to declare functions in Section 7.4.

Consider the console application in Fig. 7.1, which uses a subroutine (invoked from the application's Main method) to print a worker's payment information.

Figure 7.1. Subroutine for printing payment information.

  1  ' Fig. 7.1: Payment.vb  2  ' Subroutine that prints payment information.  3  Module Payment  4     Sub Main()  5        ' call subroutine PrintPay 4 times  6        PrintPay(40, 10.5)                  7        PrintPay(38, 21.75)                 8        PrintPay(20, 13)                    9        PrintPay(30, 14)                   10     End Sub ' Main 11 12     ' print dollar amount earned in console window             13     Sub PrintPay(ByVal hours As Double, ByVal wage As Decimal) 14        ' pay = hours * wage                                    15        Console.WriteLine("The payment is {0:C}", hours * wage) 16     End Sub ' PrintPay                                         17  End Module ' Payment 

The payment is $420.00 The payment is $826.50 The payment is $260.00 The payment is $420.00 



The program contains two method declarations. Lines 410 define method Main, the subroutine that executes when the console application is loaded. Lines 1316 define method PrintPay, a subroutine that executes when it is called from method Main.

Main makes four calls (lines 69) to subroutine PrintPay, causing PrintPay to execute four times. Although the method arguments in this example are constants, recall that arguments can also be variables or expressions. For example, the statement

 PrintPay(employeeExtraHours, employeeWage * 1.5) 


could be used to display payment information for an employee who is being paid time-and-a-half for working overtimethis statement calculates only the overtime portion of the person's pay.

When Main calls PrintPay, the program makes a copy of the value of each argument (e.g., 40 and 10.5 in line 6), and program control transfers to the first line of method PrintPay. Method PrintPay receives the copied values and stores them in the parameters hours and wage. Then PrintPay calculates hours * wage and displays the result, using the currency format (line 15). When the End Sub statement (line 16) is encountered, control is returned to the next statement in the calling method, Main.

The first line of method PrintPay (line 13) shows that PrintPay declares a Double parameter hours and a Decimal parameter wage. These parameters hold the values passed to PrintPay so that they can be accessed within this method. Note that the entire declaration of method PrintPay appears within the body of module Payment.

Subroutine Declarations

The format of a subroutine declaration is


Sub method-name(parameter-list)
      declarations and statements
End Sub

Subroutines in previous versions of Visual Basic were referred to as Sub procedures because of the use of keyword Sub. The parameter-list is a comma-separated list in which the subroutine declares each parameter variable's type and name. There must be one argument in the method call for each parameter in the method header (we will see an exception to this when we study Optional parameters in Section 7.18). The type of each argument must be consistent with its corresponding parameter's type, i.e., Visual Basic must be able to implicitly convert the value of the argument to a value of the parameter's type. For example, a parameter of type Double could receive a value of 7.35, 22 or 0.03546, but not "hello", because a Double variable cannot contain a String. In Section 7.9 we discuss this issue in detail. If a method does not receive any values, the parameter list is empty (i.e., the method name is followed by an empty set of parentheses).

Common Programming Error 7.1

Declaring a variable in the method's body with the same name as a parameter variable in the method header is a compilation error.


Error-Prevention Tip 7.1

Although it is allowable, an argument passed to a method should not have the same name as the corresponding parameter name in the method declaration. This prevents ambiguity that could lead to logic errors.


The declarations and statements in the method declaration form the method body. The method body contains code that performs actions, generally by manipulating or processing the method's parameters. The body of a method declared with Sub must be terminated with keywords End Sub. The method body is also referred to as a block. A block is a group of declarations and executable statements.

Control returns to the next statement in the caller when execution reaches the End Sub statement in the called method.

Software Engineering Observation 7.4

Method names tend to be verbs because methods typically perform actions. By convention, method names begin with an uppercase first letter. For example, a method that sends an e-mail message might be named SendMail.


Error-Prevention Tip 7.2

Small methods are easier to test, debug and understand than large methods.


Good Programming Practice 7.1

When a parameter is declared without a type, its type is assumed to be Object. Explicitly declaring a parameter's type improves program clarity.




Visual BasicR 2005 for Programmers. DeitelR Developer Series
Visual Basic 2005 for Programmers (2nd Edition)
ISBN: 013225140X
EAN: 2147483647
Year: 2004
Pages: 435

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net