Private Sub Form_Click() Dim a As Integer,b As Integer,k As Integer a = 2 b = 4 k = fun((a),b)+fPrivate Sub Form_Click()Dim a As Integer,b As Integer,k As Integera = 2b = 4k = fun((a),b)+fun(a,b)Print kEnd SubPrivate Function fun(n As Integer,m As In
来源:学生作业帮助网 编辑:作业帮 时间:2024/11/27 08:23:44
Private Sub Form_Click() Dim a As Integer,b As Integer,k As Integer a = 2 b = 4 k = fun((a),b)+fPrivate Sub Form_Click()Dim a As Integer,b As Integer,k As Integera = 2b = 4k = fun((a),b)+fun(a,b)Print kEnd SubPrivate Function fun(n As Integer,m As In
Private Sub Form_Click() Dim a As Integer,b As Integer,k As Integer a = 2 b = 4 k = fun((a),b)+f
Private Sub Form_Click()
Dim a As Integer,b As Integer,k As Integer
a = 2
b = 4
k = fun((a),b)+fun(a,b)
Print k
End Sub
Private Function fun(n As Integer,m As Integer) As Integer
Static k As Integer
n = n + m
k = k + n
m = m + k
fun = n + m
print fun;n;m
End Function
答案是第一行16 6 10第二行40 12 28 第三行 56
我不理解的是为什么M是12而不是16呢?
Private Sub Form_Click() Dim a As Integer,b As Integer,k As Integer a = 2 b = 4 k = fun((a),b)+fPrivate Sub Form_Click()Dim a As Integer,b As Integer,k As Integera = 2b = 4k = fun((a),b)+fun(a,b)Print kEnd SubPrivate Function fun(n As Integer,m As In
Private Function fun(n As Integer, m As Integer) As Integer
首先先说下你的函数 其中 n 和 m 是按址传递的,也就是如果你在函数内改变了他的值,其真实的值也变了
begin:
a=2,b=4
fun((a), b),先计算这个
n=n+m;其计算时为 a=a+b 这时a=6了,这就是按址传递
k=k+n; k=0+a k=6
m=m+k; b=b+k b=10
fun=n+m fun=a+b fun=16
print fun;n;m print fun;a,b 16 6 10
这时的a=6 b=10
然后如上再代入 +fun(a,b)
.
最好你可以调试着运行,看一下值的变化
static 申请的变量,值的存活时间是程序的结束,所以,二次调用时k的值不是0