Private Sub Form_click()
Dim x As Integer, y As Integer, z As Integer
x = 1: y = 2: z = 3
Call proc(x, x, z)
Print x; x; z
Call proc(x, y, y)
Print x; y; y
End Sub
Private Sub proc(x As Integer, y As Integer, z As Integer)
x = 3 * z
y = 2 * z
z = x + y
End Sub运行之后:
6 6 12
6 10 10
Dim x As Integer, y As Integer, z As Integer
x = 1: y = 2: z = 3
Call proc(x, x, z)
Print x; x; z
Call proc(x, y, y)
Print x; y; y
End Sub
Private Sub proc(x As Integer, y As Integer, z As Integer)
x = 3 * z
y = 2 * z
z = x + y
End Sub运行之后:
6 6 12
6 10 10
Static i As Integer
Do
a(i) = a(i) + a(i + 1)
i = i + 1
Loop While i < 2
End Sub
Private Sub command1_click()
Dim m As Integer, i As Integer, x(10) As Integer
For i = 0 To 4: x(i) = i + 1: Next i
For i = 0 To 2: Call proc(x): Next i
For i = 0 To 4: Print x(i): Next i
End Sub
还有这个题目
结果为什么是
3
5
7
9
5
1
2
3
4
5
1
2
3
4
5
vb默认的参数传递是引用方式(ByRef)执行Call proc(x, x, z)时
x = 3 * z ' x=9
y = 2 * z ' y=6 这一条语句其实改变 x 的值,相当于 x = 2 * z
z = x + y '相当于 z = x + x
Private Sub proc(x As Integer, y As Integer, z As Integer)和from_click中的
Dim x As Integer, y As Integer, z As Integer的变量定义问题
改成
Dim x As Integer, y As Integer, z As IntegerPrivate Sub Form_click()x = 1: y = 2: z = 3
Call proc(x, x, z)
Debug.Print x; x; z
Call proc(x, y, y)
Debug.Print x; y; y
End Sub
Private Function proc(a As Integer, b As Integer, c As Integer)
x = 3 * c
y = 2 * c
z = x + y
End Function
显示结果:
9 9 15
18 12 12
第二个把Static i As Integer去了点三次CLICK显示结果:
16
11
3
4
5
16
11
3
4
5
16
11
3
4
5
Dim x As Integer, y As Integer, z As Integer
x = 1: y = 2: z = 3
Call proc(x, x, z)
Print x; x; z
Call proc(x, y, y)
Print x; y; y
End Sub
Private Sub proc(x As Integer, y As Integer, z As Integer)
x = 3 * z
y = 2 * z
z = x + y
End Sub分析一下:
第一次传入Proc(x, x, z)时,在Proc()函数内部看来,x和y(Proc内部的x和y)都是指向x(Proc外的x)的指针。所以无论是对x还是y(指Proc()内部)进行操作,返回的都是x(Proc()外)。所以……你自己分析分析吧,我自己是觉得很清晰,将起来真的好麻烦 :)
proc(x,x,z)byref
x=3*z 9 2 3
y=2*z 2*z=6 2 3
z=x+y 6 2 6+6=12
printx;x;z result 6,6,12
第二次:
proc(x,y,y) 6 2 12
x=3*z 3*2=6 2 12
y=2*z 6 2*2=4 12
z=x+y 6 6+4=10
print x,y,y result 6,10,10