Visual Basic程序问题

来源:百度知道 编辑:UC知道 时间:2024/06/29 23:32:00
1.
Function Func(a As Integer, b As Integer) As Integer
Static m As Integer, i As Integer
m = 0
i = 2
i = i + m + 1
m = i + a + b
Func = m

End Function

Private Sub Command1_Click()
Dim k As Integer, m As Integer
Dim p As Integer
k = 4
m = 1
p = Func(k, m)
Print p
p = Func(k, m)
Print p

End Sub

这个程序输出的是两个8

Sub inc(a As Integer)
Static x As Integer
x = x + a
Print x
End Sub

Private Sub Command1_Click()
inc 2
inc 3
inc 4
End Sub

那么为什么这个程序输出的是 2 5 9?
请帮我分析下这两个程序~~

2. Private Sub Command1_Click()
Dim a(5)
For i = 0 To 4
a(i) = i + 1
t = i + 1
If t = 3 Then
Print a(i);
a(t - 1) = a(i - 2)
Else
a(t) = a(i

第一个问题
i = i + m + 1 i=2+0+1=3
m = i + a + b m=3+4+1=8
Func = m 返回8

第二个问题
Static 是静态变量类似于全局变量,所以上一次函数运行的结果还会保留下来。从原理上讲,过程里的dim是局部变量,内存分配在栈中,过程调用过就清空了,而静态变量和全局变量放在程序的静态内存区,过程调用完不会被清空