1、Visual Basic 程序设计,主讲教师:杜云-西南大学计算机与信息科学学院E-mail:Dyun_jx163.Com,第 6 章,过程,过程概述将程序分割成较小的逻辑部件就可以简化程序设计任务。称这些部件为过程。过程可用于压缩重复任务,例如,压缩频繁的计算。例如:求1!+2!+n!,求1!+2!+3!+N!,Dim i As Integer Dim j As Integer sum = 0 n = InputBox(请输入一个数N) For i = 1 To nk = 1For j = 1 To ik = k * jNext jsum = sum + kNext iPrint “1!+2
2、!+3!+.“ & n & “!“ & “=“ & sum,一般的方法,Public k As Long Public Sub fact(ByRef n As Integer) k = 1 For j = 1 To nk = k * i Next End Sub,求阶乘的过程,n = InputBox(“请输入一个数n“) sum = 0 For i = 1 To nCall fact(i)sum = sum + k NextPrint “1!+2!+3!+.“ & n & “!“ & “=“ & sum,调用过程求 1!+2!+n!,Dim n As Integer Dim sum As L
3、ong Dim i As Integer,1/1!+2/2!+N/N!,Private Sub Command2_Click() n = Val(InputBox(“请输入一个数n“) sum = 0 For i = 1 To nCall fact(i)sum = sum + i / k NextPrint “1/1!+2/2!+.“ & n & “/“ & n & “!“ & “=“ & sum End Sub,用过程编程有两大好处:1:过程可使程序划分成离散的逻辑单元,每个单元都比无过程的整个程序容易调试。2:一个程序中的过程,往往不必修改或只需稍作改动,便可以成为另一个程序的构件。VB中
4、有两类过程,一类是事件过程,另类是通用过程。事件过程是由系统提供的、不需要用户创建。通用过程是由用户根据自己的需要定义和编写的、可供事件过程多次调用的程序段。通用过程又可分为:Sub子过程 Function过程(函数过程),6.2 SUB子过程的定义与调用,Sub子过程格式: Private | Public Static Sub 过程名 (参数表) 语句组Exit Sub语句组 End Sub,其创建方法有两种:一是在代码窗口中直接按规定的格式编写;二是利用VB的过程添加工具生成过程框架,然后编写过程体。,可以将子过程放入标准模块、类模块和窗体模块中。按照缺省规定,所有模块中的子过程为 Pu
5、blic(公用的),这意味着在应用程序中可随处调用它们。,2:使用添加过程对话框创建过程:,打开代码窗口,工具菜单,添加过程,事例:编写求N的累加和的SUB过程,Private Sub Command2_Click() Text1.Text = “ Text2.Text = “ Text1.SetFocus End Sub,Public s As Integer Public Sub sumzr(n As Integer)Dim i As Integers = 0For i = 1 To ns = s + iNext End Sub,Private Sub Command1_Click()Ca
6、ll sumzr(Val(Text1.Text)Text2.Text = s End Sub,Public Sub sumzr(n As Integer, s As Integer)Dim i As Integers = 0For i = 1 To ns = s + iNext End Sub,Private Sub Command1_Click()Dim n As Integer, s As IntegerCall sumzr(Val(Text1.Text), s)Text2.Text = s End Sub,3:过程的调用,使用Call语句格式:CALL (实际参数) 直接使用过程名 格式
7、: 实际参数,过程 编写事例:该过程的功能是让计算机发出”梆梆梆”的声音,发声次数由调用过程时传递的参数控制.该过程带有一个形式参数.,Sub beepsound(n As Integer)Dim i As IntegerDim j As DoubleFor i = 1 To nBeepFor j = 1 To 100000Next jNext i End Sub,分析该过程的作用范围? 注意过程创建的方法、创建的位置、参数的使用,调用beepsound过程,Private Sub Command1_Click()Call beepsound(Val(Text1.Text) End Sub,注
8、意调用格式,Private Sub Command1_Click()beepsound Val(Text1.Text) End Sub,例6.4,Sub MyMove(ByVal Tag%)Label1.Left = Label1.Left + Tag * 100Label1.Top = Label1.Top + Tag * 100 End Sub, 事件过程调用MyMove: Private Sub Command1_Click()Dim k%If Rnd 0.5 Then k = 1 Else k = -1MyMove (k) End Sub,事例:编写求N!的SUB过程,Public S
9、ub fact(n As Integer, s As Long)Dim i As Integers = 1For i = 1 To ns = s * iNext End Sub,事例:编程计算1!+2!+n!,Private Sub Command1_Click()Dim i As Integer, s As Long, sum As LongFor i = 1 To Val(Text1.Text)Call fact(i, s)sum = sum + sNextText2.Text = sum End Sub,Visual Basic 包含内部的函数,如 Sqr、Cos 或 Chr。此外,还可
10、用 Function 语句编写自己的 Function 过程。 1:函数过程的语法是 Public|PrivateStatic Function 函数名 (形参表)As 数据类型语句组函数名 = 表达式Exit Function 语句组函数名 = 表达式End Function 与 Sub 过程一样,Function 过程也是一个独立的过程,可读取参数、执行一系列语句并改变其参数的值。 与子过程不同,Function 过程可返回一个值到调用的过程。,6.1 Function 过程,事例:编写求N!的FUNCTION过程,Public K As Long Function FACT(N As I
11、nteger) As Long K = 1 For I = 1 To N K = K * I Next I FACT = K End Function,Private Sub Command1_Click()Dim N As IntegerDim S As LongN = Val(Text1.Text)S = FACT(N)Text2.Text = S End Sub,2 Function过程的调用,格式:变量名=Function过程名(参数列表),Private Sub Command1_Click()Dim PR As Boolean, M As LongM = Val(Text1.Tex
12、t)PR = Prime(M)If PR = True Then Picture1.Print Str(M) + “是素数“If PR = False Then Picture1.Print Str(M) + “不是素数“ End Sub,Option Explicit Private Function Prime(M As Long) As BooleanDim i As IntegerPrime = True 向函数名赋值 If M 2 ThenFor i = 2 To Sqr(M)If M Mod i = 0 Then Prime = False: Exit ForNext End If
13、 End Function,注意调用格式,分析函数返回值的传递,例编写求1+2+3+N 的函数过程,Public m As Long Function funcsum(n As Integer) As Long m = 0 For i = 1 To n m = m + i Next funcsum = m End Function,Private Sub Command2_Click() Dim n As Integer Dim sum As Long n = Val(Text1.Text) sum = funcsum(n) Text2.Text = sum End Sub,分别用SUB过程和
14、FUNCTION过程计算 S=1!+2!+N!,Private Sub Form_Load() Label1.Caption = “输入一个数 N“ Label2.Caption = “1!+2!+.+N!=“ Text1.Text = “ Text2.Text = “ Command1.Caption = “计算“ End SubOption Explicit Public Sub FACT(n As Integer, s As Long)Dim i As Integers = 1For i = 1 To ns = s * iNext End Sub,该过程可以放在标准模块中,Private
15、 Sub Command1_Click()Dim n As Integer, s As LongDim i As Integer, sum As Longn = Val(Text1.Text)For i = 1 To nCall FACT(i, s)sum = sum + sNextText2.Text = sum End Sub,调用过程计算阶乘累加和,分别用SUB过程和FUNCTION过程计算 S=1!+2!+N!,Public Function fact1(n As Integer) As Long Dim i As Integer, s As Long s = 1 For i = 1
16、To ns = s * i Next fact1 = s End Function,注意函数过程如何才能实现返回值到调用过程,Private Sub Command1_Click() Dim n As Integer, i As Integer Dim x As Long Dim sum As Long n = Val(Text1.Text) For i = 1 To n x = fact1(i) sum = sum + x Next Text2.Text = sumEnd Sub,Function Area(x!, y!, z!) As SingleDim c!c = (x + y + z)
17、 / 2Area = Sqrt(c * (c - x) * (c - y) * (c - z) End Function,例6.1, 在事件过程中输入数据,分别调用计算三角形面积的函数过程,然后显示总面积 Sub Form_Click()Dim a!, b!, c!, d!, e!, f!, g!, s1!, s2!, s3!a = InputBox(“输入a“): b = InputBox(“输入b“): c = InputBox(“输入c“)d = InputBox(“输入d“): e = InputBox(“输入e“): f = InputBox(“输入f“)g = InputBox(“
18、输入g“)s1 = Area(a, b, c)s2 = Area(c, d, e)s3 = Area(e, f, g)MsgBox (“多边形面积=“ & s1 + s2 + s3) End Sub,Function gcd%(ByVal m%, ByVal n%)If m 0)m = n: n = r: r = m Mod nLoopgcd = n End Function,例6.2,辗转相除法是利用以下性质来确定两个正整数a和b的最大公因子的: 1.ab,令r为所得余数(0rb)。 若r=0,算法结束;b即为答案。 2.互换:置ab,br,并返回第一步。,传递参数的方式有两种:,6.3 参
19、数的传递,按地址传递参数,按值传递参数,形式参数与实际参数,sub过程和function函数过程中的参数称为形式参数(简称形参)在程序中调用sub过程和function函数过程时的参数称为实际参数(简称实参),1. 按位置传送 按位置传送就是实参的次序与形参的次序应匹配,位置次序一 一对应。2.传值与传址 ByVal:可选项。表示该参数按值传递。此时实参的值不随形参值的变化而变化。 ByRef:可选项。表示该参数按地址传递。此时实参的值随形参值的变化而变化,ByRef 是Visual Basic的默认选项。,Call Area(50, 20)Sub Area(Length, Width),按地
20、址传递参数是把实参变量的内存地址传递给形参变量 。在定义过程时,用ByRef说明的形参,或缺省传递方式说明,都是按地址传递。,Public Function f1(ByRef x As Integer) As Integerx = x * xf1 = x End FunctionPrivate Sub Command1_Click()Dim n As IntegerDim s As Integern = Val(Text1.Text)s = f1(n)Print “s=“; sPrint “n=“; n End Sub,由于参数按地址传递,过程中对形式参数的改变将改变实际参数的值, ? 程序的
21、输出结果是?,例如,编写一个交换两变量值的函数过程Swap: Private Function Swap(x As Integer, y As Integer)Dim tmp As Integertmp = x: x = y: y = tmp End Function 编写窗体的Click事件过程,调用该函数过程: Private Sub Form_Click()Dim a As Integer, b As Integera = 10: b = 15Print “调用交换函数过程前:a=“; a, “b=“; bSwap a, bPrint “调用交换函数过程后:a=“; a, “b=“; b
22、 End Sub,分析参数传递方式对实际参数的影响,按值传递参数按值传递参数是把实参变量的值传递给形参变量,实参变量的值在过程调用前后保持不变。定义过程时用ByVal关键字指出参数是按值来传递的。 例如:Sub Fun(ByVal x as Integer) End Sub,Public Function f2(ByVal x)x = x * xf2 = x End Function,Private Sub Command1_Click() Dim n As Integer Dim s As Integer n = Val(Text1.Text) s = f2(n) Print “s=“; s
23、 Print “n=“; n End Sub,由于参数按值传递,过程中对形式参数的改变不会改变实际参数的值, ? 程序的输出结果是?,例如,把上面调用交换变量值的函数过程Swap修改为按值传递参数: Private Function Swap(ByVal x As Integer, ByVal y As Integer)Dim tmp As Integertmp = x: x = y: y = tmp End Function Private Sub Form_Click()Dim a As Integer, b As Integera = 10: b = 15Print “调用交换函数过程前
24、:a=“; a, “b=“; bSwap a, b 按值传递参数Print “调用交换函数过程后:a=“; a, “b=“; b End Sub,分析参数传值方式对实际参数的影响?,显示按值传递参数没有能实现两变量值的交换。,本章作业分析,1 Public Sub mysub4(x As Integer)x = 3 * x - 1If x 5 Then x = x + 9Print x End Sub Private Sub Form_Click()x = 1mysub4 3+XPrint x End Sub,注意调用过程是把3+X的值4传递给形式参数X,而非将实际参数X传递个形式参数X,请分
25、析输出结果,将调用过程改为: Private Sub Form_Click() Dim x As Integerx = 1mysub4 xPrint x End Sub分析结果?,Private Sub Form_Click()Dim b As Integer, y As IntegerCall Mysub2(3, b)y = bCall Mysub2(4, b)Print y + b End Sub,1、 Public Sub Mysub2(x, t)t = 0For k = 1 To xt = t + kNext End Sub,Y=6 (1+2+3),B=10(1+2+3+4),y +
26、b=16,2. Public Sub Mysub3(ByVal x As Integer, y As Integer)x = y + xy = x Mod yEnd Sub,Private Sub Form_Click()Dim a As Integer, b As Integera = 11: b = 22Call Mysub3(a, b)Print a; b End Sub,A- 11 B- 11,3、 Function x(ByVal m As Long) As LongDim k As Long, num As Integerk = 1num = mDo While num = 1k
27、= k * (num Mod 10)num = num 10Loopx = k End Function Private Sub Command1_Click()Dim n As Long, r As Longn = Val(Text1.Text)r = x(n)Print r End Sub,程序运行时,通过文本框输入数值225,程序的输出是?,编程作业,1、有10个数分别为23、45、16、32、91、78、86、48、65、28,要求分别用选择排序法和冒泡排序法将这10个数降序排列输出。2、编写一个判断素数的sub子过程,过程名为prime,要判断的数在调用该过程时通过参数传递。3、编写一个求N!的Function过程。N的值在调用该过程时通过参数传递。过程名为fact,