VB.Net - 函數(shù)

2022-05-24 16:20 更新

過程是一組調(diào)用時一起執(zhí)行任務(wù)的語句。執(zhí)行該過程之后,控制返回到調(diào)用過程的語句。 VB.Net有兩種類型的程序:

  • Functions

  • Sub procedures or Subs

函數(shù)返回一個值,而Subs不返回值。

定義函數(shù)

函數(shù)語句用于聲明函數(shù)的名稱,參數(shù)和主體。 函數(shù)語句的語法是:

[Modifiers] Function FunctionName [(ParameterList)] As ReturnType
    [Statements]
End Function
  • Modifiers 修飾符 :指定函數(shù)的訪問級別;可能的值有:公共,私有,保護,朋友,關(guān)于保護超載,重載,共享和陰影朋友和信息。

  • FunctionName:表示該函數(shù)的名稱

  • ParameterList 參數(shù)列表 :指定參數(shù)的列表

  • ReturnType 返回類型 :指定變量的函數(shù)返回的數(shù)據(jù)類型

示例

以下代碼片段顯示了一個函數(shù)FindMax,它接受兩個整數(shù)值,并返回兩個較大者。

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

函數(shù)返回值

在VB.Net中,函數(shù)可以通過兩種方式向調(diào)用代碼返回一個值:

  • 通過使用return語句

  • 通過將值分配給函數(shù)名

下面的例子演示了如何使用FindMax函數(shù):

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

當(dāng)上述代碼被編譯和執(zhí)行時,它產(chǎn)生了以下結(jié)果:

Max value is : 200

遞歸函數(shù)

一個函數(shù)可以調(diào)用自身。 這被稱為遞歸。 以下是使用遞歸函數(shù)計算給定數(shù)字的階乘的示例:

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

當(dāng)上述代碼被編譯和執(zhí)行時,它產(chǎn)生了以下結(jié)果:

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

參數(shù)數(shù)組

有時,在聲明函數(shù)或子過程時,您不確定作為參數(shù)傳遞的參數(shù)的數(shù)量。 VB.Net param數(shù)組(或參數(shù)數(shù)組)在這些時候來幫助。
以下示例演示了這一點:

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

當(dāng)上述代碼被編譯和執(zhí)行時,它產(chǎn)生了以下結(jié)果:

The sum is: 2938

傳遞數(shù)組作為函數(shù)參數(shù)

您可以在VB.Net中將數(shù)組作為函數(shù)參數(shù)傳遞。 以下示例演示了這一點:

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

當(dāng)上述代碼被編譯和執(zhí)行時,它產(chǎn)生了以下結(jié)果:

Average value is: 214.4

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號