180 likes | 375 Views
过程和函数. 200 9 - 4 - 1. Subroutines and Functions. Indivisible units of code that contains a group of statements that perform a predefined activity and that should be activated as one unit.
E N D
过程和函数 2009-4-1
Subroutines and Functions • Indivisible units of code that contains a group of statements that perform a predefined activity and that should be activated as one unit. • Subroutines are called and return no value while functions are activated by assigning the value that they return. • Both sub-routines and functions are in fact methods.
Subroutines • Invoked, that is activated, either with the Call statement or just by name. • Subroutines are defined with the Substatement. • A subroutine may receive parameters, corresponding to the arguments are defined in the Sub statement. Call One() One() PrivateSub mySub(ByRef argument1 AsInteger, _ ByVal argument2 AsInteger)
ByRef and ByVal • When an argument is defined ByVal • 过程从形式参数接受值,不带回值(不知道变量的地址)。 • When an argument is defined ByRef • 能带回参数值(知道变量的地址)。
ByVal指的是按值传递,程序会将被传递的参数的值拷贝一份存在一个临时区域中参与调用过程的执行,在代码中即使使用a=a+1改变参数值的语句,实际只改变了它的副本内容,而并没有真正改变a的内容;ByVal指的是按值传递,程序会将被传递的参数的值拷贝一份存在一个临时区域中参与调用过程的执行,在代码中即使使用a=a+1改变参数值的语句,实际只改变了它的副本内容,而并没有真正改变a的内容; • ByRef,是指按地址传递参数,是缺省的方式。在被调用时传递给调用函数的是其地址,在调用函数中对这个参数做出的任何改变都直接改变了它的值,程序会根据参数存放的地址直接对它进行修改,即使跳出被调函数或过程这个改变还是永久的。
Example Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim p1, p2 As Integer Call mySub(p1, p2) MsgBox("Parameter 1 is " & p1) MsgBox("Parameter 2 is " & p2) End Sub Private Sub mySub(ByRef argument1 As Integer, _ ByVal argument2 As Integer) argument2 = 10 '传值 argument1 = 10 '传地址 MsgBox("Argument 1 is " & argument1) MsgBox("Argument 2 is " & argument2) End Sub Argument 1 is 10 Argument 2 is 10 Parameter 1 is 10 Parameter 2 is 0
建立SUB过程的方法 • 通用过程:在模块中建立 • .VB(Module-End Module) • 步骤 • 项目菜单 • 添加模块 • 添加新项 • 在模板中选择”模块” • ……
练习1 • 在主程序读取INPUTBOX中的输入生成数据个数 • 在子程序中生成这个个数的随机数,并将这些数的最大\最小\平均值返回主程序,并显示出来.
参数数组 • 当需要数量不确定的参数时,可声明一个“参数数组”。 • ByRef/ByVal 形参数组 as 类型 • 允许过程接受参数的值数组,不需知道参数数组中的元素数。 • 每次过程调用都单独确定数组的大小。 • reDim/或赋值
事件过程 Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click Dim x(3) As Single Dim y(4) As Integer Dim i As Integer Call mySub3(x, y) For i = 0 To UBound(x) lstX.Items.Add(x(i)) Next For i = 0 To UBound(y) lstY.Items.Add(y(i)) Next End Sub
过程 Public Sub mySub3(ByRef a() As Single, ByVal b() As Integer) Dim i As Integer For i = 0 To UBound(a) Randomize() a(i) = 10 * Rnd() Next For i = 0 To UBound(b) Randomize() b(i) = 20 * Rnd() Next End Sub
Private and Public Subroutines • When the subroutine is • Private • 只在声明的范围内有作用。 • Protected • 在它的类或继承来的类范围内有效。 • Friend • It is accessible from anywhere within the assembly that their class is defined within. • Public • It is accessible also beyond the class it is defined within. • When the subroutine is Shared, there is no need to instantiate it before executing it. • A subroutine can be Shared whether it is Private, Protected,Friend or Public.
函数 • 也是一个过程,但是要返回值,返回的值可以是任何类型 • 调用方法: • 赋值语句 • Return
Example ' Activate the function Dim I AsInteger = Factorial(10) PrivateFunction Factorial(ByVal intNumber AsInteger) _ AsDouble Dim I AsInteger Dim dblSum AsDouble = 1 If intNumber > 0 Then For I = 1 To intNumber dblSum = dblSum * I Next Return (dblSum) Else Factorial = 1 EndIf EndFunction
Recursive Functions(递归函数) • 自己调用自己的函数 PrivateFunction factorial(ByVal dblNumber AsDouble) AsDouble If dblNumber <= 1 Then Return (1) Else Return (dblNumber * factorial(dblNumber - 1)) EndIf EndFunction
变量的作用域 • 局部变量 • 块级别 • 过程级别 • 静态变量 • STATIC • 模块变量(代码容器) • PUBLIC • FRIEND
练习2 • PAGE 120 • 2(1)、(2) • PAGE 118 • 5.6.3 • 5.6.4
练习3 • 建立一个过程,完成两个点画线任务 • 检验坐标是否出界,若出界,放弃本次画线任务 • 画线(对象\方法) • 在主程序中随机确定起点,并在500MS的间隔之后产生新的点,每次是上一个点坐标做为起点,新产生的坐标作为终点. • 任务: • 过程和参数 • 产生点 • 事件 • 过程调用