Coding FunctionsYou can write your own general-purpose function procedures that are not tied to specific events. You can call these functions from any Visual Basic application just as you can subroutine procedures. Function procedures work just like subroutine procedures in every way; you call them from elsewhere in the code. Unlike subroutine procedures, however, a function procedure always returns a value. If you run across a needed calculation and Visual Basic has no built-in function equivalent, you can write your own function that returns that calculated value. When you call the function, you must do something with the returned value. You cannot put a function call on a line by itself as you can with a subroutine. If CalcTax() is a function, you cannot call the function like this: CalcTax () Problem! The CalcTax() function will return a value and you must do something with that value. Therefore, youll usually assign the return value like this: lblAmt.Caption = CalcTax() Okay You can also use the function call inside an expression, like this: curAmount = Estimate * .2 + CalcTax() * .14
The functions that you write arent quite as built-in as Visual Basics built-in functions, but they behave the same way. Your functions never become part of VBs repertoire, but you can put them in any module that needs to access them. Over time, you will write many general-purpose function and subroutine procedures and you might want to keep a module library of common routines that youll use throughout different applications. To use one of the procedures that you write, you can add that procedures module to whatever application needs the procedure. You will write new function procedures the same way you write new subroutine procedures (with Tools | Add Procedure or by typing the first function procedures line at the end of the module). Use the Function keyword in place of Sub. The following statements would code the beginning and ending statements from a CalcTax() function: Public Function CalcTax () As Single End Function Youll notice something extra on that functions opening statement: As Single. In addition to using the Function keyword, you must also specify the functions return value datatype in the functions opening declaration line. Therefore, this CalcTax() function returns a single-precision datatype. Listing 13.2 contains a function that computes the postage for a letter or package using the following rules: 1. The post office charges 32 cents for 8 ounces or less. 2. Add 15 cents for each 4 ounces above the first 8. 3. The weight cannot exceed 24 ounces. The functions code assumes that the letter or package weight appears in a text box control named txtWeight.Text. In addition, the weight must appear as ounces. Therefore, any application that uses this function must make sure these conditions are met before calling the function.
Listing 13.2. Calculating postage with a function procedure. 1: Public Function Postage() As Currency 2: Calculate postage based on the 3: weight of a letter or package 4: Dim curPostHold As Currency 5: Dim intWeight As Integer 6: Dim intPress As Integer MsgBox() return 7: 8: Grab the weight from the text box 9: and convert to number for comparison 10: intWeight = Val(txtWeight.Text) 11: 12: Select Case intWeight 13: Case Is <= 8: curPostHold = 0.32 14: 15: Case Is <= 12: curPostHold = 0.47 16: 17: Case Is <= 16: curPostHold = 0.62 18: 19: Case Is <= 20: curPostHold = 0.77 20: 21: Case Is <= 24: curPostHold = 0.92 22: 23: Case Is > 24: 24: intPress = MsgBox(Weight is too heavy, _ 25: vbExclamation, Error) 26: curPostHold = 0# 27: End Select 28: 29: Postage = curPostHold Return the value 30: End Function Listing 13.2 demonstrates the way you return the value from a function. There is no Postage variable declared, yet the second-to-last line assigns a value to Postage. Postage is the name of the function, not a variable! Inside a function procedure, when you assign a value to the functions name, the function uses that value as the return value. This function doesn't actually end until the End Function statement is reached, but the return value is set right before the terminating statement.
|
|
![]() |
![]() |
|||