函式程序 (Visual Basic)Function Procedures (Visual Basic)

AFunction程序是一系列的 Visual Basic 陳述式加上FunctionEnd Function陳述式。A Function procedure is a series of Visual Basic statements enclosed by the Function and End Function statements. Function程序執行的工作,然後將控制權傳回呼叫程式碼。The Function procedure performs a task and then returns control to the calling code. 當它傳回控制項時,它也會傳回值,呼叫程式碼。When it returns control, it also returns a value to the calling code.

此程序呼叫時,它的陳述式執行時,每次開始之後的第一個可執行陳述式Function陳述式,並與第一個結束End FunctionExit Function,或Return陳述式時發生。Each time the procedure is called, its statements run, starting with the first executable statement after the Function statement and ending with the first End Function, Exit Function, or Return statement encountered.

您可以定義Function模組、 類別或結構中的程序。You can define a Function procedure in a module, class, or structure. 它是Public根據預設,這表示您可以從任何位置呼叫它可存取模組、 類別或結構定義它的應用程式中。It is Public by default, which means you can call it from anywhere in your application that has access to the module, class, or structure in which you defined it.

AFunction程序可以取得引數,例如常數、 變數或運算式,這會傳遞給它所呼叫的程式碼。A Function procedure can take arguments, such as constants, variables, or expressions, which are passed to it by the calling code.

宣告語法Declaration Syntax

宣告的語法Function程序如下所示:The syntax for declaring a Function procedure is as follows:

[Modifiers] Function FunctionName [(ParameterList)] As ReturnType  
    [Statements]  
End Function  

修飾詞可以指定存取層級和多載、 覆寫,將分享,與遮蔽的相關資訊。The modifiers can specify access level and information regarding overloading, overriding, sharing, and shadowing. 如需詳細資訊,請參閱 < Function 陳述式For more information, see Function Statement.

您所進行的相同方式宣告的每個參數Sub 程序You declare each parameter the same way you do for Sub Procedures.

資料類型Data Type

每個Function程序中的資料類型,只要為每個變數。Every Function procedure has a data type, just as every variable does. 此資料型別由As子句中的Function陳述式,並判斷函式傳回給呼叫程式碼之值的資料類型。This data type is specified by the As clause in the Function statement, and it determines the data type of the value the function returns to the calling code. 下列範例宣告可說明這點。The following sample declarations illustrate this.

Function yesterday() As Date  
End Function  
  
Function findSqrt(ByVal radicand As Single) As Single  
End Function  

如需詳細資訊,請參閱 「 組件 」 中Function 陳述式For more information, see "Parts" in Function Statement.

傳回值Returning Values

Function程序會傳送回到呼叫端程式碼會呼叫它的傳回值。The value a Function procedure sends back to the calling code is called its return value. 程序會傳回此值中有兩種:The procedure returns this value in one of two ways:

  • 它會使用Return陳述式來指定傳回值,並傳回立即控制權傳輸至呼叫端程式。It uses the Return statement to specify the return value, and returns control immediately to the calling program. 下列範例將說明這點。The following example illustrates this.
Function FunctionName [(ParameterList)] As ReturnType  
    ' The following statement immediately transfers control back  
    ' to the calling code and returns the value of Expression.  
    Return Expression  
End Function  
  • 它將值指派給自己的程序的一或多個陳述式中的函式名稱。It assigns a value to its own function name in one or more statements of the procedure. 控制項不會傳回給呼叫程式,直到Exit FunctionEnd Function陳述式。Control does not return to the calling program until an Exit Function or End Function statement is executed. 下列範例將說明這點。The following example illustrates this.
Function FunctionName [(ParameterList)] As ReturnType  
    ' The following statement does not transfer control back to the calling code.  
    FunctionName = Expression  
    ' When control returns to the calling code, Expression is the return value.  
End Function  

將傳回的值指派給函式名稱的優點是,控制項不會傳回從程序直到遇到Exit FunctionEnd Function陳述式。The advantage of assigning the return value to the function name is that control does not return from the procedure until it encounters an Exit Function or End Function statement. 這可讓您指派的初步的值,並視需要稍後調整它。This allows you to assign a preliminary value and adjust it later if necessary.

如需有關傳回值的詳細資訊,請參閱Function 陳述式For more information about returning values, see Function Statement. 如需傳回陣列的詳細資訊,請參閱陣列For information about returning arrays, see Arrays.

呼叫語法Calling Syntax

您可以叫用Function程序包括其名稱和引數是在指派陳述式或運算式的右側。You invoke a Function procedure by including its name and arguments either on the right side of an assignment statement or in an expression. 您必須提供值不是選擇性的所有引數,您必須將引數清單括在括號中。You must provide values for all arguments that are not optional, and you must enclose the argument list in parentheses. 如果已不提供任何引數,您可以選擇性地省略括號。If no arguments are supplied, you can optionally omit the parentheses.

呼叫語法Function程序如下所示:The syntax for a call to a Function procedure is as follows:

lvalue = functionname [( argumentlist )]lvalue = functionname [( argumentlist )]

If (( functionname [( argumentlist )] / 3) <= expression ) ThenIf (( functionname [( argumentlist )] / 3) <= expression ) Then

當您呼叫Function程序中,您就不必使用它的傳回值。When you call a Function procedure, you do not have to use its return value. 如果您不這樣做,函式的所有動作皆都執行,但傳回的值會被忽略。If you do not, all the actions of the function are performed, but the return value is ignored. MsgBox 通常稱為以這種方式。MsgBox is often called in this manner.

宣告和呼叫的圖例Illustration of Declaration and Call

下列Function程序會計算已知值的其他兩個邊直角三角形斜邊的最長的側邊。The following Function procedure calculates the longest side, or hypotenuse, of a right triangle, given the values for the other two sides.

Function Hypotenuse(side1 As Double, side2 As Double) As Double
    Return Math.Sqrt((side1 ^ 2) + (side2 ^ 2))
End Function

下列範例示範的典型呼叫hypotenuseThe following example shows a typical call to hypotenuse.

Dim testLength, testHypotenuse As Double
testHypotenuse = Hypotenuse(testLength, 10.7)

另請參閱See also