在VB中,F(xiàn)unction函數(shù)用于定義和執(zhí)行可返回值的子例程。
Function函數(shù)的語(yǔ)法如下:
[Modifiers] Function FunctionName [(ParameterList)] [As ReturnType]
[Statements]
[FunctionName = Expression]
[Exit Function]
[Statements]
[FunctionName = Expression]
[Exit Function]
...
[Return Expression]
End Function
其中:
以下是一個(gè)簡(jiǎn)單的示例,演示了如何使用Function函數(shù):
Public Function AddNumbers(ByVal num1 As Integer, ByVal num2 As Integer) As Integer
Dim sum As Integer
sum = num1 + num2
Return sum
End Function
在上述示例中,AddNumbers函數(shù)接受兩個(gè)整數(shù)參數(shù)num1和num2,并返回它們的和。