单选题 编写如下程序:
Private Sub Command1_Click()
Dim m As Integer,n As Integer
n=2
For m=1 To 3
Print proc(n);
Next m
End Sub
Function proc(i As Integer)
Dim a As Integer
Static b As Integer
a=a+1
b=b+1
proc=a*b+i
End Function
程序运行后,单击命令按钮Command1,输出结果为______。
【正确答案】 B
【答案解析】[解析] 变量b定义成Static即静态变量,特点是每次调用都会保留上次的值。程序Click事件中执行了3次proc(n),即proc(2):
第一次,a=a+1=0+1=1,b=b+1=0+1=1,proc=a*b+i=1*1+2=3;
第二次,a=a+1=0+1=1,b=b+1=1+1=2,proc=a*b+i=1*2+2=4;
第三次,a=a+1=0+1=1,b=b+1=2+1=3,proc=a*b+i=1*3+2=5;
输出的结果为3 4 5。因此本题答案为B。