VB.Net函式


一個過程是一組語句,在呼叫時一起執行任務。過程執行後,控制權返回到呼叫過程的語句。 VB.Net有兩種型別的程式:

  • 函式
  • 子程式或Subs

重要區別: 函式返回一個值,而Subs不返回任何一個值。

定義函式

Function語句用於宣告函式的名稱,引數和函式體。Function語句的語法是:

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

其中,

  • Modifiers - 指定函式的存取級別; 可能的值有:Public, Private, Protected, Friend, Protected Friend以及有關過載,覆蓋,共用和投影的資訊。
  • FunctionName - 表示函式的名稱。
  • ParameterList: 指定引數的列表。
  • ReturnType: 指定函式返回的變數的資料型別。

範例

下面的程式碼片段顯示了一個函式: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