一個過程是一組語句,在呼叫時一起執行任務。過程執行後,控制權返回到呼叫過程的語句。 VB.Net有兩種型別的程式:
重要區別: 函式返回一個值,而Subs不返回任何一個值。
Function
語句用於宣告函式的名稱,引數和函式體。Function
語句的語法是:
[Modifiers] Function FunctionName [(ParameterList)] As ReturnType
[Statements]
End Function
其中,
Public
, Private
, Protected
, Friend
, Protected Friend
以及有關過載,覆蓋,共用和投影的資訊。下面的程式碼片段顯示了一個函式:FindMax
,它取兩個整數值作為引數並返回兩個中較大的那一個。
Function FindMax(ByVal num1 As Integer, ByVal num2 As Integer) As Integer
' local variable declaration '
Dim result As Integer
If (num1 > num2) Then
result = num1
Else
result = num2
End If
FindMax = result
End Function
在VB.Net中,函式可以通過兩種方式將值返回給呼叫程式碼:
return
語句以下範例演示如何使用FindMax
函式:
Module FunctionReturnValue
Function FindMax(ByVal num1 As Integer, ByVal num2 As Integer) As Integer
' local variable declaration '
Dim result As Integer
If (num1 > num2) Then
result = num1
Else
result = num2
End If
FindMax = result
End Function
Sub Main()
Dim a As Integer = 100
Dim b As Integer = 200
Dim res As Integer
res = FindMax(a, b)
Console.WriteLine("Max value is : {0}", res)
Console.ReadLine()
End Sub
End Module
執行上面範例程式碼,得到以下結果 -
F:\worksp\vb.net\function>vbc FunctionReturningValue.vb
F:\worksp\vb.net\function>FunctionReturningValue.exe
Max value is : 200
一個函式可以呼叫它自己(自身),這被稱為遞回函式。以下是一個使用遞回函式計算給定數位的階乘的範例:
Module Recursive
Function factorial(ByVal num As Integer) As Integer
' local variable declaration '
Dim result As Integer
If (num = 1) Then
Return 1
Else
result = factorial(num - 1) * num
Return result
End If
End Function
Sub Main()
'calling the factorial method '
Console.WriteLine("Factorial of 6 is : {0}", factorial(6))
Console.WriteLine("Factorial of 7 is : {0}", factorial(7))
Console.WriteLine("Factorial of 8 is : {0}", factorial(8))
Console.ReadLine()
End Sub
End Module
執行上面範例程式碼,得到以下結果 -
F:\worksp\vb.net\function>vbc Recursive.vb
Microsoft (R) Visual Basic Compiler version 14.0.1038
for Visual Basic 2012
Copyright (c) Microsoft Corporation. All rights reserved.
....
F:\worksp\vb.net\function>Recursive.exe
Factorial of 6 is : 720
Factorial of 7 is : 5040
Factorial of 8 is : 40320
有時,在宣告一個函式或子過程的時候,有時不確定傳遞的引數的數量。 VB.Net引數陣列(或引數陣列)可以解決這個問題。
以下範例演示了這一點:
Module ParamArrays
Function AddElements(ParamArray arr As Integer()) As Integer
Dim sum As Integer = 0
Dim i As Integer = 0
For Each i In arr
sum += i
Next i
Return sum
End Function
Sub Main()
Dim sum As Integer
sum = AddElements(111, 720, 222, 567, 999)
Console.WriteLine("The sum is: {0}", sum)
Console.ReadLine()
End Sub
End Module
執行上面範例程式碼,得到以下結果 -
F:\worksp\vb.net\function>vbc ParamArrays.vb
Microsoft (R) Visual Basic Compiler version 14.0.1038
for Visual Basic 2012
Copyright (c) Microsoft Corporation. All rights reserved.
......
F:\worksp\vb.net\function>ParamArrays.exe
The sum is: 2619
可以在VB.Net中傳遞一個陣列作為函式的引數。以下範例演示了這一點:
Module PassingArrays
Function getAverage(ByVal arr As Integer(), ByVal size As Integer) As Double
'local variables'
Dim i As Integer
Dim avg As Double
Dim sum As Integer = 0
For i = 0 To size - 1
sum += arr(i)
Next i
avg = sum / size
Return avg
End Function
Sub Main()
' an int array with 5 elements '
Dim balance As Integer() = {1110, 210, 320, 179, 509}
Dim avg As Double
'pass pointer to the array as an argument
avg = getAverage(balance, 5)
' output the returned value '
Console.WriteLine("Average value is: {0} ", avg)
Console.ReadLine()
End Sub
End Module
執行上面範例程式碼,得到以下結果 -
F:\worksp\vb.net\function>vbc PassingArrays.vb
Microsoft (R) Visual Basic Compiler version 14.0.1038
for Visual Basic 2012
Copyright (c) Microsoft Corporation. All rights reserved.
... ...
F:\worksp\vb.net\function>PassingArrays.exe
Average value is: 465.6