看看这简单的VB代码?我有个问题不明白?

来源:百度知道 编辑:UC知道 时间:2024/06/25 05:11:15
Private Function fun(x As Integer) As Integer
Dim i As Integer, s As Integer
For i = 1 To x
s = s + i
Next i
fun = s

End Function

Private Sub Command1_Click()
Dim s As Integer
s = fun(100)
Print s

End Sub

本来是:"Private Function fun()" 可这里为什么有“(x As Integer) As Integer”,特别是那个特号外面的as integer不明白

Private Function fun(x As Integer) As Integer
Private----表示为私有成员(函数)
Function----表示为函数
fun----为函数名
()----内部为形参列表,当没有参数时可以省略,参数可以多个用","分开
x As Integer----为一个形参x ,类型为Integer
As Integer ----表示函数有返回值,且返回的类型为Integer; 值为函数体内第一次为函数名(fun)所赋的值

hpfrog是对的 但是我说的通俗点

Private Function fun(x As Integer) As Integer就是定义一个函数 函数名字叫fun

这个函数是带参数的 你调用的时候用
a=fun(100)调用
fun(x As Integer) 里面这个as integer是你传入的参数类型
你传入参数时候fun(a)就不正确了

后面的as integer是返回值得类型
就是a是整数型的 你才能这样赋值

说明白就是
Dim fun as Integer
外面那个是定义fun为整数类型

函数的返回值是integer类型的