VisualBasic 程序设计 vb7教程.pptVIP

  • 1
  • 0
  • 约1.72万字
  • 约 150页
  • 2017-08-19 发布于广东
  • 举报
Private Sub form_click() Dim x As Integer, y As Integer, z As Integer x = 1: y = 2: z = 3 Call procl(x, y, z) Call procl(x, y, z) End Sub Private Sub procl(x As Integer, y As Integer, z As Integer) x = 3 * x : y = 2 * z : z = x + y Print x; y; z End Sub A.6 6 12 B. 9 6 15 6 10 10 6 10 10 C. 3 6 9 D. 9 10 10 9 18 27 9 10 15 例如: Function Fun(x as integer ,y as integer) as integer x=x+y If x0 Then Fun=x Else Fun=y End if End Function Private Sub command1_Click() Dim a as integer,b as integer A=2:b=3 Text1.text=Fun(a,b) Text2.text=Fun(a,b) Text2.text=Fun(a,b) End Sub Text1.text=? Text2.text=? Text2.text=? 你知道吗? 例如: Function Fun(x as integer ,y as integer) as integer x=x+y If x0 Then Fun=x Else Fun=y End if End Function Private Sub command1_Click() Dim a as integer,b as integer A=2:b=3 Text1.text=Fun(a,b) Text2.text=Fun(a,b) Text2.text=Fun(a,b) print fun(a,b);a;b End Sub Text1.text=5 Text2.text=8 Text2.text=11 结果为: 原因: A=2:b=3 Text1.text=Fun(a,b) A=5:b=3 Text2.text=Fun(a,b) A=8:b=3 text1.text=5 text2.text=8 Text3.text=Fun(a,b) A=11:b=3 text3.text=11 在编写程序时,有时我们要利用副作用,比如对数组进行操作时,有时我们则要避免这种副作用。 一般来说,传地址比传值更能节省内存和提高效率。但是应注意它的副作用。 传值: 传值就是通过值传送实际参数,即传送实参的值而不是传送它的地址。在这种情况下,系统把需要传送的变量复制到一个临时单元中,然后把该临时单元的地址传送给被调用的通用过程。由于通用过程没有访问变量(实参)的原始地址,因而不会改变原来变量的值,所有的变化都是在变量的副本上进行。 在VB中,传值方式通过关键字ByVal来实现。也就是说,在定义通用过程时,如果形参前面有关键字ByVal,则该参数用传值方式传送,否则用引用(即传地址)方式传送。 b a 100 200 sub testsub(ByVal x, ByVal y) x=x+10 y=y+10 End sub Sub form_click() a=100:b=200 Call testsub(a, b) Print a,b End sub 1 6 例: x y b a 100 20 100 200 200 sub testsub(ByVal x, ByVal y) x=x+10 y=y+10 End sub

您可能关注的文档

文档评论(0)

1亿VIP精品文档

相关文档