课程表

VB.Net基本教程

VB.Net高级教程

工具箱
速查手册

VB.Net - 函数

当前位置:免费教程 » 程序设计 » VB.Net

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

  • Functions

  • Sub procedures or Subs

函数返回一个值,而Subs不返回值。


定义函数

函数语句用于声明函数的名称,参数和主体。 函数语句的语法是:

  1. [Modifiers] Function FunctionName [(ParameterList)] As ReturnType
  2. [Statements]
  3. End Function
  • Modifiers 修饰符 :指定函数的访问级别;可能的值有:公共,私有,保护,朋友,关于保护超载,重载,共享和阴影朋友和信息。

  • FunctionName:表示该函数的名称

  • ParameterList 参数列表 :指定参数的列表

  • ReturnType 返回类型 :指定变量的函数返回的数据类型


示例

以下代码片段显示了一个函数FindMax,它接受两个整数值,并返回两个较大者。

  1. Function FindMax(ByVal num1 As Integer, ByVal num2 As Integer) As Integer
  2. ' local variable declaration */
  3. Dim result As Integer
  4. If (num1 > num2) Then
  5. result = num1
  6. Else
  7. result = num2
  8. End If
  9. FindMax = result
  10. End Function


函数返回值

在VB.Net中,函数可以通过两种方式向调用代码返回一个值:

  • 通过使用return语句

  • 通过将值分配给函数名


下面的例子演示了如何使用FindMax函数:

  1. Module myfunctions
  2. Function FindMax(ByVal num1 As Integer, ByVal num2 As Integer) As Integer
  3. ' local variable declaration */
  4. Dim result As Integer
  5. If (num1 > num2) Then
  6. result = num1
  7. Else
  8. result = num2
  9. End If
  10. FindMax = result
  11. End Function
  12. Sub Main()
  13. Dim a As Integer = 100
  14. Dim b As Integer = 200
  15. Dim res As Integer
  16. res = FindMax(a, b)
  17. Console.WriteLine("Max value is : {0}", res)
  18. Console.ReadLine()
  19. End Sub
  20. End Module


当上述代码被编译和执行时,它产生了以下结果:

  1. Max value is : 200


递归函数

一个函数可以调用自身。 这被称为递归。 以下是使用递归函数计算给定数字的阶乘的示例:

  1. Module myfunctions
  2. Function factorial(ByVal num As Integer) As Integer
  3. ' local variable declaration */
  4. Dim result As Integer
  5. If (num = 1) Then
  6. Return 1
  7. Else
  8. result = factorial(num - 1) * num
  9. Return result
  10. End If
  11. End Function
  12. Sub Main()
  13. 'calling the factorial method
  14. Console.WriteLine("Factorial of 6 is : {0}", factorial(6))
  15. Console.WriteLine("Factorial of 7 is : {0}", factorial(7))
  16. Console.WriteLine("Factorial of 8 is : {0}", factorial(8))
  17. Console.ReadLine()
  18. End Sub
  19. End Module


当上述代码被编译和执行时,它产生了以下结果:

  1. Factorial of 6 is: 720
  2. Factorial of 7 is: 5040
  3. Factorial of 8 is: 40320


参数数组

有时,在声明函数或子过程时,您不确定作为参数传递的参数的数量。 VB.Net param数组(或参数数组)在这些时候来帮助。

以下示例演示了这一点:

  1. Module myparamfunc
  2. Function AddElements(ParamArray arr As Integer()) As Integer
  3. Dim sum As Integer = 0
  4. Dim i As Integer = 0
  5. For Each i In arr
  6. sum += i
  7. Next i
  8. Return sum
  9. End Function
  10. Sub Main()
  11. Dim sum As Integer
  12. sum = AddElements(512, 720, 250, 567, 889)
  13. Console.WriteLine("The sum is: {0}", sum)
  14. Console.ReadLine()
  15. End Sub
  16. End Module


当上述代码被编译和执行时,它产生了以下结果:

  1. The sum is: 2938


传递数组作为函数参数

您可以在VB.Net中将数组作为函数参数传递。 以下示例演示了这一点:

  1. Module arrayParameter
  2. Function getAverage(ByVal arr As Integer(), ByVal size As Integer) As Double
  3. 'local variables
  4. Dim i As Integer
  5. Dim avg As Double
  6. Dim sum As Integer = 0
  7. For i = 0 To size - 1
  8. sum += arr(i)
  9. Next i
  10. avg = sum / size
  11. Return avg
  12. End Function
  13. Sub Main()
  14. ' an int array with 5 elements '
  15. Dim balance As Integer() = {1000, 2, 3, 17, 50}
  16. Dim avg As Double
  17. 'pass pointer to the array as an argument
  18. avg = getAverage(balance, 5)
  19. ' output the returned value '
  20. Console.WriteLine("Average value is: {0} ", avg)
  21. Console.ReadLine()
  22. End Sub
  23. End Module


当上述代码被编译和执行时,它产生了以下结果:

  1. Average value is: 214.4
转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号