VB.Net - 函式



過程是一組語句,當被呼叫時,這些語句一起執行一項任務。過程執行完成後,控制權將返回到呼叫該過程的語句。VB.Net 有兩種型別的過程:

  • 函式

  • 子過程或 Subs

函式返回值,而子過程不返回值。

定義函式

Function 語句用於宣告函式的名稱、引數和主體。Function 語句的語法如下:

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

其中,

  • 修飾符 - 指定函式的訪問級別;可能的值包括: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 myfunctions
   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

當以上程式碼編譯並執行時,會產生以下結果:

Max value is : 200

遞迴函式

函式可以呼叫自身。這稱為遞迴。以下是一個使用遞迴函式計算給定數字的階乘的示例:

Module myfunctions
   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

當以上程式碼編譯並執行時,會產生以下結果:

Factorial of 6 is: 720
Factorial of 7 is: 5040
Factorial of 8 is: 40320

Param 陣列

有時,在宣告函式或子過程時,您不確定作為引數傳遞的引數數量。VB.Net Param 陣列(或引數陣列)在這種情況下很有幫助。

以下示例演示了這一點:

Module myparamfunc
   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(512, 720, 250, 567, 889)
      Console.WriteLine("The sum is: {0}", sum)
      Console.ReadLine()
   End Sub
End Module

當以上程式碼編譯並執行時,會產生以下結果:

The sum is: 2938

將陣列作為函式引數傳遞

您可以在 VB.Net 中將陣列作為函式引數傳遞。以下示例演示了這一點:

Module arrayParameter
   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() = {1000, 2, 3, 17, 50}
      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

當以上程式碼編譯並執行時,會產生以下結果:

Average value is: 214.4
廣告

© . All rights reserved.