VB 题 悬赏20

来源:百度知道 编辑:UC知道 时间:2024/05/24 15:20:40
窗体上有text1,text2两个文本框及一个命令按钮
command1,编写下列程序
Dim y As Integer

Private Sub Command1_Click()
Dim x As Integer
x = 2
Text1.Text = fun2(fun1(x), y)
Text2.Text = fun1(x)
End Sub

Private Function fun1(x As Integer) As Integer
x = x + y: y = x + y
fun1 = x + y
End Function
Private Function fun2(x As Integer, y As Integer) As Integer
fun2 = 2 * x + y
End Function
当单击1次和单击2次命令按钮后,文本框text1和text2内的值分别是
问题补充:
请详细分析一下,本人初学

注意,x为局部变量,值不会保留,
而y是全局变量,值保留
单击1次
Text1.Text = fun2(fun1(x), y)
先调用fun1(x),传址调用,会改变x
x=x+y: y = x + y
x=2,y=2
返回fun1=4
再调用fun2(4,y)
fun2 = 2 * 4 + y=2*4+2=10
Text1.Text 为10
Text2.Text = fun1(x)
x=x+y=2+2=4,y=x+y=4+2=6
返回fun1=10
Text2.Text 为10

单击2次
Text1.Text = fun2(fun1(x), y)
先调用fun1(x),传址调用,会改变x
x=x+y: y = x + y
x=2+6=8,y=8+6=14
返回fun1=22
再调用fun2(22,y)
fun2 = 2 * 22 + y=2*22+14=58
Text1.Text 为58
Text2.Text = fun1(x)
x=x+y=8+14=22,y=x+y=22+14=36
返回fun1=58
Text2.Text 为58