170 likes | 336 Views
21 世纪高等学校计算机基础教育系列教材. 第 9 章 Visual Basic 中的过程和函数 . 人民邮电出版社. 定义过程. 分成两类: sub 过程 (子过程) 和 function 过程 (函数过程) 定义子过程方法: [ private ][ static ][ public ] sub 过程名 [ (参数表) ] 语句块 [exit sub] [ 语句块 ] End sub 例如: Private sub print_test () print “ this is a sub procedure! ” End sub
E N D
21世纪高等学校计算机基础教育系列教材 第9章 Visual Basic中的过程和函数 • 人民邮电出版社
定义过程 • 分成两类:sub过程(子过程)和function过程(函数过程) • 定义子过程方法: [ private ][ static ][ public ] sub 过程名 [(参数表)] 语句块 [exit sub] [语句块] End sub 例如: Private sub print_test() print “this is a sub procedure!” End sub 参数表:可以分为值传递和地址传递,默认情况下是地址传递,要进行值传递,可以用byval来进行设置。详细内容在后面例子中讲解。
定义过程 Sub tryout(x as interger,byval y as interger) x=x+100 y=y*6 Print x,y End sub • 建立sub过程的方法(A): 1)工程添加模块新建模块(图1) 2)工具添加过程(如图2窗口) 3)填写名称、设置类型、适用范围,确定回到图1所示窗口。 • 建立sub过程的方法(B): 工程添加模块,键入过程名称(假设china) Sub china() ‘键入内容后回车,end sub自动出现 End Sub (见图3) 在两行之间就可以输入代码。输入代码要用到所学的各种可能的程序语句 例如if结构,while结构,select case结构等,以及一些语句,如print等等 1 2 3
调用过程 • 把过程作为一个语句来使用 过程名 实际参数 例如 tryout(a,b) • Call语句调用 Call 过程名[(实际参数)] 例如 call tryout(a,b) • 例如:计算矩形面积的过程 Sub recarea(rlen as single,rwid as single ) dim area as single area=rlen *rwid msgbox “the area of this rectangle is” & area End sub Sub form_click() dim a,b a=inputbox (“input the length”) b=inputbox (“input the width”) call recarea(val(a),val(b)) ‘也可以直接用 recarea val(a),val(b) End sub Val(a)函数,将数字字符串a转化成数字,如val( “123”)=123
通用过程和事件过程 • 附加在窗体和控件上的过程就叫做事件过程 例如:command1_click() 、Form _ load() 通用过程就是我们在前面所讲解定义的过程,可以放在标准模块中,也可以放置窗体模块中,事件过程只能放置在窗体模块中,不同的模块中的过程可以互相调用;过程名唯一的清况下,直接用过程名称调用,如果两个模块中含有相同名称的过程,则需要有模块名.过程名的方式来调用。 • 可以看出,这些过程都是没有返回值的,也就是说,在运行该过程后,就不再使用这些过程的计算结果。这种过程不返回值,只能做为语句使用。如果一个过程要有返回值,可以出现在表达式中,那么就要用到function过程(在有些书中叫做函数) 比如: msgbox 在语句msgbox “北方工业大学欢迎您!” 中是简单的sub过程 而在表达式 a=msgbox(“北方工业大学欢迎您!”)中则是function过程 • 这种现象叫做多态*
Function过程(函数) • 建立function过程 [ private ][ static ][ public ] function 过程名 [(参数表)] 语句块 [过程名=表达式] [exit function] [语句块] End function • 例如:求最大公约数的函数过程: Function gcd (byval x as integer, byval y as integer) as integer do while y<>0 reminder=x mod y x=y y=reminder Loop Gcd=x End function Sub Form_click() dim a as integer, b as integer ,c a=inputbox(a) b=inputbox(B) C= gcd(val(a),VAL(b)) Print a & "和 " & b & "的最大公约数是" & c End sub 运行输入25和65得到如图结果
Function过程(函数) • 调用function过程作为一个语句 • 可以放在赋值语句中,也可以放在打印语句中,也可以放在函数中作为参数,例如: • 求123,564,1002,以及321的最大公约数 • 可以通过以下方法来实现: Sub form_activate() dim a,b,c,d,ab,cd a=123:b=564:c=1002:d=321 ab=gcd(a,b) cd=gcd(c,d) print gcd(ab,cd) End sub
参数 • 形参和实参 • 形参是在sub或function过程定义中出现的参数表,实参则是在调用过程中给定的参数表。例如: Function gcd (byval x as integer, byval y as integer) as integer do while y<>0 reminder=x mod y x=y y=reminder Loop Gcd=x End function Sub Form_click() dim a as integer, b as integer ,c a=inputbox(a) b=inputbox(B) C= gcd(val(a),VAL(b)) Print a & "和 " & b & "的最大公约数是" & c End sub Sub recarea(rlen as single,rwid as single ) dim area as single area=rlen *rwid msgbox “the area of this rectangle is” & area End sub Sub form_click() dim a,b a=inputbox (“input the length”) b=inputbox (“input the width”) call recarea(val(a),val(b)) End sub 上面绿色字符是形参,蓝色字符是实参,以及前面例子中的参数
参数传递方式 • 按位置传递 实际参数和形式参数的次序必须相匹配,即位置必须一致 传送时,名称不要求一致,但是参数个数必须相同,位置也必须相同(主要是类型) 形参各变量间用逗号隔开,变量可以是: 字符串,数组名(带有左右括号) 实际参数可以是: 常数、表达式、变量名、数组名(带有左右括号) 设:sub testsub(a as integer,array() as single,recvar as rectype,c as string) Type rectype rand as string*12 serialnum as long End type Dim recv as rectype Call testsub(x,a(),recv,”dephone”) • 指名传递 就是把实参显式地指定给形参,用:=将形参和实参连接起来。例如: Sub addsum(first,second,third) c=(first+second)*third print c End sub Addsum 4,6,8与 addsum first:=4,second:=6,third:=8 addsum second:=6,first:=4,third:=8中的任何依据等价 addsum third:=8,second:=6, first:=4
参数传递 • 参数按照两种方法传递(传地址和传值) • 传地址也叫做引用,默认情况下均为传地址;传值只传递实际参数,这种情况下,系统把需要传递的变量复制到一个临时单元中,然后该临时单元的地址被传递给过程,保证原实参地址中的值不发生改变。 看下面例子: sub power (x as integer, byval y as integer) x=x+100 y=y*6 print “x=“;x, ”y=”;y end sub Sub command2_click() dim a as integer, b a=100:b=23 print “the powers:” power a, b print “the command:” print “a=”;a, ” b=”;b End sub
例题辨析 Private Sub Command1_Click() Dim x As Integer Dim y As Integer x = 3 y = 4 r = sum(x, y) Print r End Sub Function sum(x As Integer, y As Integer, Optional z) sum = x + y If Not IsMissing(z) Then sum = sum + z End If End Function • Static sub s1(byval x as integer,byval y as integer) • dim z as integer • z=z+5 • y=x+z • print z;x; • End sub • Private sub command1_click() • X=10 • y=20 • call s1(x,y) • x=y-x • s1 y,x • End sub • 运行窗体点击命令按钮1的结果是: • A 5 10 5 20 B 5 10 5 10 • C 5 10 10 10 D 5 10 5 5
参数传递 • 数组参数的传递 • 数组作为参数时,只能传地址,不能传值 • 使用数组参数时,要在数组名后加上括号(),以区别于一般变量参数 • 例如 call s (p (), q),数组作为参数时,形参和实参使用同一段内存地址 • 也就是说,假设形参是P(),实参是q(),则调用后p(1)=q(1)…… • 求数组最大值: Private function findmax(a() as integer dim start asinteger,finish as integer dim I as integer start=lbound(a) Finish=ubound(a) max=a(start) for i=start to finish if a(i)>max then max=a(i) next I Findmax=max End function Sub form_click() redim b(4) as interger b(1)=30 b(2)=80 b(3)=234 b(4)=874 c=findmax(b()) print c End sub
可选参数和可变参数 • 可选参数 • 11页例子中,参数中的z前有一个optional,表示该参数可选,在调用时可以通过ismissing()函数来判断其有无。 • 例如:sub multi (first, sec, optional third) • n=first*sec • if not ismissing(third) then n=n*third • print n • Private sub form_click() • multi 10,20 //结果200 • End sub • Private sub command1_click() • multi 10,20,3 //结果600 • End sub • 可变参数 • Print函数可以输出任意多个数据,输出的数据就是它的参数,他是一个可变参数的函数。Vb中可以建立这种可变参数的过程,用paramarray命令来定义,格式如下: Sub 过程名 ( paramarray 数组名) • 数组名是一个形式参数,只有括号和名称,无上下界,省略变量类型(默认variant),可以和任意类型的变量相匹配。 举例见上面文本框 Sub multi(paramarray num()) n=1 for each I in num n=n*I next I Print n
对象参数 • Vb允许用对象(窗体、控件最为参数) • 用对象作参数语法如下: sub 过程名(形参表) 语句 exit sub end sub • 窗体作参数 • 例如四个窗体的大小、位置一致时,可以用函数来设定 Sub setform (formnum as form) formnum.left=2000 formnum.top=3000 formnum.width=2000 formnum.height=2000 End sub • 调用该函数的语句 • formset form1 相当于 form1.left=2000 form1.top=3000 form1.width=2000 form1.height=2000
对象参数 • 控件参数 • Sub 过程名 (参数名 as control) • 语句 • End sub • 可以通过 typeof 参数名 is 控件类型 来进行进一步的判断,从而对不同类型的类型进行不同的设置。也可以避免在调用出现错误 • 控件类型的关键字: • CheckBox(复选框) frame(框架) ComboBox(组合框) • HScrollBar(水平滚动条) Label(标签) CommandButton(命令按钮) • ListBox(列表框) Menu(菜单) DirListBox(目录列表框) • DriveListBox (驱动器列表框) FileListBox(文件列表框) • OptionButton(单选按钮) Timer(计时器) TextBox(文本框) PictureBox(图片框) VscroolBar(纵向滚动条)
局部内存分配 • 对于全局变量,vb在运行时就为它们分配内存,对于局部变量,由于这些变量的使用不确定,所以只能动态地进行分配内存(使用时分配,用完后马上释放),但是对于频繁使用的局部变量,这种方法不好,可以通过将这类变量定义为静态变量的方法来解决该问题,语法如下: • Static 变量表 详细说明见P248 • 例如: • Static I as integer • i=0 • Sub Timer1_timer() • i=i+1 • End sub
Shell 函数 • Shell函数用来调用各种应用程序的。 • 语法如下: • shell(应用程序名 [,窗口类型]) • 应用程序名包括文件名,路径、扩展名,扩展名有.com, .exe, .bat / .pif ,其他文件不能这样调用 • 窗口类型:见右表