VB.Net - 函数

VB.Net - 函数

❮ 上一节

下一节 ❯

VB.Net - 函数

过程是一组在调用时一起执行任务的语句。 执行过程后,控制返回到调用该过程的语句。 VB.Net有两类程序 −

函数

子程序或子程序

函数返回值,而 Sub 不返回值。

定义函数

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

' 局部变量声明 */

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

' 局部变量声明 */

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

' 局部变量声明 */

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

参数数组

有时,在声明函数或子过程时,您不确定作为形参传递的参数数量。

VB.Net 参数数组(或参数数组)在这些时候会派上用场。

以下示例演示了这一点 −

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()

' 具有 5 个元素的 int 数组 '

Dim balance As Integer() = {1000, 2, 3, 17, 50}

Dim avg As Double

'将指针作为参数传递给数组

avg = getAverage(balance, 5)

' 输出返回值 '

Console.WriteLine("Average value is: {0} ", avg)

Console.ReadLine()

End Sub

End Module

当上面的代码被编译并执行时,会产生以下结果 −

Average value is: 214.4

❮ 上一节

下一节 ❯

💡 关键要点

❮ 上一节 下一节 ❯ VB.Net - 函数 过程是一组在调用时一起执行任务的语句。 执行过程后,控制返回到调用该过程的语句。 VB.Net有两类程序 −

更多疯狂内容

制药职业配方

制药职业配方

🔥 135 📅 10-24
涨价!中国移动咪咕视频钻石会员价格最新调整:连续包月 20 元、包年 188 元
《英雄联盟》转区需要多久(lol转区时间说明)