当前位置: 首页 > 教程 > VB.Net >

VB.Net函数

精华
小牛编辑
145浏览
2023-03-14

一个过程是一组语句,在调用时一起执行任务。过程执行后,控制权返回到调用过程的语句。 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