End Function
Function 过程中使用的变量分为两类:一类在过程中显式声明,另一类则不是。Variables used in Functionprocedures fall into two categories: those that are explicitly declared withinthe procedure and those that are not.
过程中显式声明(使用 Dim 或等效语句)的变量始终是该过程的局部变量。Variables that are explicitlydeclared in a procedure (using Dim or the equivalent) are always localto the procedure. 在过程中使用但未显式声明的变量依然是局部变量,除非在过程外某些更高级别显式声明这些变量。Variables that are used but notexplicitly declared in a procedure are also local unless they are explicitlydeclared at some higher level outside the procedure.
过程可以使用未在过程中显式声明的变量,但如果在模块级别定义的任何内容具有相同的名称,则会发生命名冲突。A procedure can use a variable that is notexplicitly declared in the procedure, but a naming conflict can occur ifanything you defined at the module level hasthe same name. 如果过程引用与其他过程、常量或变量具有相同名称的未声明变量,则假定该过程引用该模块级别名称。If your procedure refers to anundeclared variable that has the same name as another procedure, constant, orvariable, it is assumed that your procedure refers to that module-level name. 显示声明变量可避免此类型的冲突。Explicitly declare variables to avoid this kind of conflict. 可以使用**Optionexplicit** 语句强制显式声明变量。You can use an Option Explicitstatement to force explicit declaration of variables.
Visual Basic 可以重新安排算术表达式以提高内部效率。Visual Basic may rearrange arithmeticexpressions to increase internal efficiency. 当函数更改相同表达式中变量的值时,应避免在算术表达式中使用Function 过程。Avoid using a Functionprocedure in an arithmetic expression when the function changes the value ofvariables in the same expression. 有关算术运算符的详细信息, 请参阅运算符。Formore information about arithmetic operators, see Operators.
示例Example
此示例使用 Function 语句声明组成 Function 过程主体的名称、参数和代码。This example uses the Functionstatement to declare the name, arguments, and code that form the body of a Functionprocedure. 上一个示例使用初始化的硬类型 Optional 参数。Thelast example uses hard-typed, initialized Optional arguments.
' The following user-definedfunction returns the square root of the
' argument passed to it.
Function CalculateSquareRoot(NumberArg As Double) As Double
If NumberArg < 0 Then' Evaluate argument.
Exit Function ' Exit to calling procedure.
Else
CalculateSquareRoot = Sqr(NumberArg) ' Returnsquare root.
End If
End Function
使用 ParamArray 关键字允许函数接受可变数量的参数。Using the ParamArray keyword enables a function to accepta variable number of arguments. 在以下定义中, 它通过值传递。Inthe following definition, it is passed by value.
Function CalcSum(ByValFirstArg As Integer,ParamArray OtherArgs())
Dim ReturnValue
' If the function is invoked asfollows:
ReturnValue = CalcSum(4, 3, 2, 1)
' Local variables are assignedthe following values: FirstArg = 4,
' OtherArgs(1) = 3,OtherArgs(2) = 2, and so on, assuming default
' lower bound for arrays = 1.
Optional 参数具有默认值和类型,而不是 Variant。Optional arguments can have default values and types other than Variant.
VB复制
' If a function's arguments aredefined as follows:
Function MyFunc(MyStr AsString,OptionalMyArg1 As _
Integer = 5,OptionalMyArg2 = "Dolly")
Dim RetVal
' The function can be invokedas follows:
RetVal = MyFunc("Hello",2, "World") ' All 3 arguments supplied.
RetVal = MyFunc("Test",, 5) ' Second argument omitted.
' Arguments one and three usingnamed-arguments.
RetVal = MyFunc(MyStr:="Hello ", MyArg1:=7)
|