注册 登录
编程论坛 VB6论坛

过程的调用

easonzgf 发布于 2014-12-11 13:07, 373 次点击
  Private Sub Command1_Click()
  Dim k As Integer, m As Integer
  Dim p As Integer
  k=4: m=1
  p=PC(k, m) : Print p;
  p=PC(k, m) : Print p
  End Sub
  Private Function PC(a As Integer, b As Integer)
  Static m As Integer, i As Integer
  m=0: i=2
  i=i + m + 1
  m=i + a + b
  PC=m
  End Function



sub inc(a as integer)
    static x as integer
      x=x+a
      print a;
end sub
private sub command1_click
    inc 2
    inc 3
    inc 4
为什么第一个程序function调用第一次是p=8,第二次结果还是8???
而在第二个程序调用sub过程中结果却是 2,5,9????

[ 本帖最后由 easonzgf 于 2014-12-11 13:11 编辑 ]
3 回复
#2
wp2319572014-12-11 13:19
静态变量这东东还是少用的好
#3
风吹过b2014-12-11 16:33
  Private Function PC(a As Integer, b As Integer)
  Static m As Integer, i As Integer
  m=0: i=2                      '在这里,重新初始化了。
  i=i + m + 1                  'i=3
  m=i + a + b                   'm= i+a+b
  PC=m                           '返回 m
  End Function

  k=4: m=1
  p=PC(k, m) : Print p;
  p=PC(k, m) : Print p

你 k ,m 是不变,所以 结果也就不变。

----------------------------------
    inc 2
    inc 3
    inc 4

sub inc(a as integer)
    static x as integer           '没有重新初始化,第一次为0 以后继续使用上一次的值
      x=x+a                   '第一次 2 ,第二次 2+3 = 5 ,第三次,5+4 = 9
      print a;
end sub
#4
easonzgf2014-12-11 18:31
回复 3楼 风吹过b
thank you very much!!!
1