返回值
...指的是
Function过程在函数完成时具有值,使得该值可以在表达式中使用或赋值给变量的能力。
函数的值可以通过三种方式返回:
'' Using the name of the function to set the return value and continue executing the function:
Function myfunc1() As Integer
myfunc1 = 1
End Function
'' Using the keyword 'Function' to set the return value and continue executing the function:
Function myfunc2() As Integer
Function = 2
End Function
'' Using the keyword 'Return' to set the return value and immediately exit the function:
Function myfunc3() As Integer
Return 3
End Function
'' This program demonstrates a function returning a value.
Declare Function myFunction () As Integer
Dim a As Integer
'Here we take what myFunction returns and add 10.
a = myFunction() + 10
'knowing that myFunction returns 10, we get 10+10=20 and will print 20.
Print a
Function myFunction () As Integer
'Here we tell myFunction to return 10.
Function = 10
End Function
返回参考文献
函数结果也可以通过引用返回,而不是通过值返回。语义是完全不同的。
通过
Function = variable或
Return variable语句赋值
Byref函数结果时,该函数不会复制并返回变量的值。相反,它返回对该变量的引用。该函数的调用者可以通过从函数返回的引用来修改变量,而无需手动使用指针。这非常像
ByRef参数。
有关详细信息,请参阅:
Byref (函数结果)
从Byref函数手动返回指针
通过在
Function = variable或
Return variable语句中的结果变量前面指定
Byval关键字,地址(通常存储在指针中)可以原样直接传递,强制
Byref函数结果引用地址指向的相同内存位置。例如:
Dim Shared i As Integer = 123
Function f( ) ByRef As Integer
Dim pi As Integer Ptr = @i
Function = ByVal pi
'' or, with RETURN it would look like this:
Return ByVal pi
End Function
Print i, f( )
参考