编写如下程序: Private Sub Command1_Click() Dim a As Integer, i As Integer a = 2 For i = 1 To 3 Print fun_s(a); Next

admin2020-06-30  25

问题 编写如下程序:
Private Sub Command1_Click()
   Dim a As Integer, i As Integer
   a = 2
   For i = 1 To 3
      Print fun_s(a);
   Next i
End Sub
Function fun_s(a As Integer) As Integer
   Dim b As Integer
   Static c As Integer
   a = a + 1
   b = b + 1
   c = c + 1
   fun_s = a + b + c
End Function
程序运行后,单击命令按钮Command1,输出结果为

选项 A、4  5  6
B、5  6  7
C、5  7  9
D、4  6  8

答案C

解析 程序初始定义全局变量a,i。a=2。在fun_s中,定义了静态变量c,能够保留上一次程序调用的值,而b不保留上次的值,每次调用函数时,自动初始化为0。利用For循环,三次调用函数fun_s,执行结果为:第一次调用函数,a = a + 1=2+1=3,b = b + 1=0+1=1, c = c + 1=0+1=1,fun_s = a + b + c=5;第二次调用函数,a = a + 1=3+1=4,b = b + 1=0+1=1,c = c + 1=1+1=2,fun_s = a + b + c=7;第三次调用函数,a = a + 1=3+1=4,b = b + 1=0+1=1,  c = c + 1=2+1=3,fun_s = a + b + c=9。故选择C项。
转载请注明原文地址:https://kaotiyun.com/show/Q6Hp777K
0

随机试题
最新回复(0)