30.07.2013 Views

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

186 Procedures Chapter 6<br />

11 PrintPay(20, 13)<br />

12 PrintPay(50, 14)<br />

13<br />

14 End Sub ' Main<br />

15<br />

16 ' print dollar amount earned in command window<br />

17 Sub PrintPay(ByVal hours As Double, ByVal wage As Decimal)<br />

18<br />

19 ' pay = hours * wage<br />

20 Console.WriteLine("The payment is {0:C}", hours * wage)<br />

21 End Sub ' PrintPay<br />

22<br />

23 End Module ' modPayment<br />

The payment is $420.00<br />

The payment is $826.50<br />

The payment is $260.00<br />

The payment is $700.00<br />

Fig. Fig. Fig. 6.2 6.2 6.2 Sub procedure for printing payment information (part 2 of 2).<br />

The program contains two procedure definitions. Lines 6–14 define Sub procedure<br />

Main, which executes when the console application is loaded. Lines 17–21 define Sub<br />

procedure PrintPay, which executes when it is invoked, or called, from another procedure,<br />

in this case Main.<br />

Main makes four calls (lines 9–12) <strong>to</strong> Sub procedure PrintPay, causing<br />

PrintPay <strong>to</strong> execute four times. Although the procedure arguments in this example are<br />

constants, arguments can also be variables or expressions. For example, the statement<br />

PrintPay(employeeOneExtraHours, employeeOneWage * 1.5)<br />

could be used <strong>to</strong> display payment information for an employee who is being paid time-anda-half<br />

for working overtime.<br />

When Main calls PrintPay, the program makes a copy of the value of each argument<br />

(e.g., 40 and 10.5 on line 9), and program control transfers <strong>to</strong> the first line of procedure<br />

PrintPay. Procedure PrintPay receives the copied values and s<strong>to</strong>res them in<br />

the parameter variables hours and wage. Then, PrintPay calculates hours * wage<br />

and displays the result, using the currency format (line 20). When the End Sub statement<br />

on line 21 is encountered, control is returned <strong>to</strong> the calling procedure, Main.<br />

The first line of procedure PrintPay (line 17) shows (inside the parentheses) that<br />

PrintPay declares a Double variable hours and a Decimal variable wage. These<br />

parameters hold the values passed <strong>to</strong> PrintPay within the definition of this procedure.<br />

Notice that the entire procedure definition of PrintPay appears within the body of<br />

module modPayment. All procedures must be defined inside a module or a class.<br />

The format of a procedure definition is<br />

Sub procedure-name(parameter-list)<br />

declarations and statements<br />

End Sub

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!