vel15.htm
来自「简单的说明如何使用VB,非常适合初学使用者,而且是用图表来解说的」· HTM 代码 · 共 1,355 行 · 第 1/3 页
HTM
1,355 行
<FONT COLOR="#000080">lblAmt.Caption = Int(Amount)</FONT></PRE><P>or you'll use the function in a calculation that needs the value like this:<BR><BR><PRE><FONT COLOR="#000080">WholePart = Int(Amount) + Estimate</FONT></PRE><P>You can write the code for your own numeric and string function procedures and add them to Visual Basic's repertoire. The functions that you write aren't quite as built in as Visual Basic's built-in functions because your functions don't become part of Visual Basic's language. Nevertheless, as with subroutine procedures, you can code the function procedures inside your application's Code window as well as store function procedures by themselves or along with subroutine procedures in external modules so that more than one application has access to the functions.<BR><P>To write a new function procedure within a Code window, whether that Code window is your application form's Code window or an external .BAS module that you've opened, you can select the View New Procedure from the menu and click the Function option button on the New Procedure dialog box. (In the previous section, you clicked Sub to open a new subroutine.) After you type a new name for the function in the Name text box, Visual Basic adds the function wrapper lines like the ones shown here for a new function procedure named MultiplyIt:<BR><PRE><FONT COLOR="#000080">Function MultiplyIt ()End Function</FONT></PRE><P>A function procedure always begins with the Function statement and ends with the End Function statement. You'll add code to the body of the function between the two wrapper lines.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Tip: </B>If you add a dollar sign after the function name, such as Reverse$(), Visual Basic assumes that you want to open a string function that will return a string value. If you omit the dollar sign, Visual Basic assumes that your function will return the Variant data type in which you can return numbers or string values. To keep things clear, always use the dollar sign for string functions.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>A function is the same in every way as a subroutine except that the function returns a value. To return the function's value, assign the return value <I>to the name of the function</I>. Don't use Call to call a function. All you do to call a function is to use the function procedure's name inside an expression or statement.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Note: </B>If you ever need to exit a function procedure before the function's normal termination—for example, if the user cancels an input box—use the Exit Function statement.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P><FONT COLOR="#FF8000"><B><I>Stop and Type: </I></B></FONT>Listing 15.3 contains a function that computes the postage for a letter or package as follows:<BR><OL><LI>The Post Office charges 32 cents for the 8 ounces.<BR><BR><LI>Add 15 cents for each additional 4 ounces above the first 8.<BR><BR><LI>The weight must be below 24 ounces.<BR><BR></OL><P>Listing 15.3 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 that a text box named txtWeight exists and holds the total package weight before calling the function.<BR><P><FONT COLOR="#FF8000"><B><I>Review: </I></B></FONT>When you want special routines that calculate or manipulate string values, and Visual Basic doesn't supply those routines through internal built-in functions such as CInt() and Mid$(), write your own function procedures. Before the function procedure terminates, assign the function's return value to the name of the function. Visual Basic uses this mechanism (the assigned name) to return the value assigned to the name back to the calling code that needs the result.<BR><P><FONT COLOR="#000080"><B>Listing 15.3. A function that calculates postage and returns the cost.</B></FONT><BR><PRE><FONT COLOR="#000080">1: Function Postage ()2: ' Calculate postage based on the3: ' weight of a letter or package4: Dim PostHold As Currency5: Dim weight As Integer6: 7: ' Grab the weight value from the text box8: ' and convert to number for comparison9: weight = Val(txtWeight.Text)10: 11: Select Case weight12: Case Is <= 8:13: PostHold = .3214: Case Is <= 12:15: PostHold = .4716: Case Is <= 16:17: PostHold = .6218: Case Is <= 20:19: PostHold = .7720: Case Is < 24:21: PostHold = .9222: Case Is >= 24:23: MsgBox "Weight is more than 24 ounces!", MB_ICONEXCLAMATION, "Error"24: PostHold = 025: End Select26: 27: Postage = PostHold ' Return the value28: End Function</FONT></PRE><P><FONT COLOR="#FF8000"><B><I>Analysis: </I></B></FONT>Line 3 defines a variable that will hold an interim postage amount throughout the Select Case. Actually, the extra PostHold variable is not strictly needed because each Case could assign directly to the function name. Nevertheless, assigning to the function name at the bottom of the function makes for better self-documentation of the code. When the function finishes, line 26 completes the function's task by assigning the calculated value to the function name.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE>Without Sharing, Your Code Is Limited: This unit's discussion of subroutines and functions can only be complete once you master the next unit's material on passing of data from procedure to procedure. Until now, a procedure could only work with data defined inside that procedure. The only exception was the function procedure that you just learned about, in which one procedure can use a value returned from another.<BR>The next unit shows you how you can share variables between functions. The controls that you've been using have been available to all procedures in your program but not the variables. Only when you learn how to pass data from one procedure to another can you see the real power of subroutine and function procedures.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><BR><A NAME="E68E117"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>Homework</B></FONT></CENTER></H3><BR><BR><A NAME="E69E104"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>General Knowledge</B></FONT></CENTER></H4><BR><OL><LI>True or false: A subroutine procedure is the same as an event procedure.<BR><BR><LI>How do you call subroutines?<BR><BR><LI>How do you call function procedures?<BR><BR><LI>Name two ways that you can begin entering a new procedure in the Code window.<BR><BR><LI>How can you determine whether a function procedure returns a string or a variant data type?<BR><BR><LI>What is the primary difference between a subroutine procedure and a function procedure?<BR><BR><LI>How do you indicate the return value of a function?<BR><BR><LI>What is the shortcut access key to move from procedure to procedure?<BR><BR><LI>How does a programmer-defined function procedure differ from a built-in function?<BR><BR><LI>What is the name for a file that holds general-purpose procedures?<BR><BR><LI>What filename extension should module files have?<BR><BR><LI>Name a limitation of the procedures given your current knowledge so far. (Hint: Read the last sidebar in this unit.)<BR><BR><LI>Name two statements that can appear in the (general) procedure.<BR><BR><LI>True or false: You can call event procedures from other event procedures.<BR><BR><LI>True or false: You can call event procedures from subroutine procedures.<BR><BR></OL><BR><A NAME="E69E105"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>Write Code That...</B></FONT></CENTER></H4><BR><OL><LI>Write the two wrapper lines for a subroutine procedure named PrReport().<BR><BR><LI>Write the two wrapper lines for a function procedure named GetValue().<BR><BR><LI>Suppose that you were writing a function named GetPi() that returned the value of the mathematical <I>pi</I> (which is approximately 3.14159). Write the statement that returned the value.<BR><BR></OL><BR><A NAME="E69E106"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>Find the Bug</B></FONT></CENTER></H4><BR><OL><LI>19. Describe what's wrong with this Call:<BR><BR>Call MySub arg1, arg2<BR><BR></OL><BR><A NAME="E69E107"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>Extra Credit</B></FONT></CENTER></H4><BR><P>Write a string-reversal function named Reverse$() that takes the string stored in the text box named txtAString and reverses the string, returning the string in the function name of Reverse.<BR><P ALIGN=LEFT><A HREF="velp07.htm" TARGET="_self"><IMG SRC="purprev.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Previous Page"></A><A HREF="#I0" TARGET="_self"><IMG SRC="purtop.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Page Top"></A><A HREF="index.htm" TARGET="_self"><IMG SRC="purtoc.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="TOC"></A><A HREF="vel16.htm" TARGET="_self"><IMG SRC="purnext.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Next Page"></A></BODY></HTML>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?