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
0 條評論,你可以發表評論,我們會進行改進
Comment author placeholder