過(guò)程是一組調(diào)用時(shí)一起執(zhí)行任務(wù)的語(yǔ)句。執(zhí)行該過(guò)程之后,控制返回到調(diào)用過(guò)程的語(yǔ)句。 VB.Net有兩種類型的程序:
Functions
Sub procedures or Subs
函數(shù)返回一個(gè)值,而Subs不返回值。
函數(shù)語(yǔ)句用于聲明函數(shù)的名稱,參數(shù)和主體。 函數(shù)語(yǔ)句的語(yǔ)法是:
[Modifiers] Function FunctionName [(ParameterList)] As ReturnType [Statements] End Function
Modifiers 修飾符 :指定函數(shù)的訪問(wèn)級(jí)別;可能的值有:公共,私有,保護(hù),朋友,關(guān)于保護(hù)超載,重載,共享和陰影朋友和信息。
FunctionName:表示該函數(shù)的名稱
ParameterList 參數(shù)列表 :指定參數(shù)的列表
ReturnType 返回類型 :指定變量的函數(shù)返回的數(shù)據(jù)類型
以下代碼片段顯示了一個(gè)函數(shù)FindMax,它接受兩個(gè)整數(shù)值,并返回兩個(gè)較大者。
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中,函數(shù)可以通過(guò)兩種方式向調(diào)用代碼返回一個(gè)值:
通過(guò)使用return語(yǔ)句
通過(guò)將值分配給函數(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í)行時(shí),它產(chǎn)生了以下結(jié)果:
Max value is : 200
一個(gè)函數(shù)可以調(diào)用自身。 這被稱為遞歸。 以下是使用遞歸函數(shù)計(jì)算給定數(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í)行時(shí),它產(chǎn)生了以下結(jié)果:
Factorial of 6 is: 720 Factorial of 7 is: 5040 Factorial of 8 is: 40320
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í)行時(shí),它產(chǎn)生了以下結(jié)果:
The sum is: 2938
您可以在VB.Net中將數(shù)組作為函數(shù)參數(shù)傳遞。 以下示例演示了這一點(diǎn):
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í)行時(shí),它產(chǎn)生了以下結(jié)果:
Average value is: 214.4
更多建議: