可怜我这个菜鸟!

[复制链接]
查看11 | 回复4 | 2009-2-19 12:52:34 | 显示全部楼层 |阅读模式
Private Sub Command1_Click()
Dim n As Integer
n = jiecheng(3)
Print n
End Sub
Public Function jiecheng(ByVal p As Integer)

Dim sum, i As Integer

sum = 1

For i = 1 To p

sum = sum * i

Next i
End Function
哥哥姐姐们,我调用一个求阶乘的子函数怎么就掉不了呢?我哪有错呢?谢谢!

回复

使用道具 举报

千问 | 2009-2-19 12:52:34 | 显示全部楼层
几个问题说一下: Public Function jiecheng(ByVal p As Integer) 改成 Public Function jiecheng(ByVal p As Integer) as integer 要声明jiecheng返回一个integer的值 Dim sum, i As Integer 的用法是不恰当的,这里sum会被声明成variant而不是想像中的interger 声明不建议用逗号连用,要写成 Dim sum as integer dim i as integer 一定要用逗号的时候要写成Dim sum as integer, i As Integer 最后是函数不返回值的...
回复

使用道具 举报

千问 | 2009-2-19 12:52:34 | 显示全部楼层
错误在于你的求阶乘的子函数有问题应该是Public Function jiecheng(ByVal p As Integer) Dim sum, i As Integer jiecheng = 1 For i = 1 To p jiecheng = jiecheng * i Next i End Function这样才能返...
回复

使用道具 举报

千问 | 2009-2-19 12:52:34 | 显示全部楼层
精简的程序来了,已经验证OK,在你程序的基本上稍作修改,你也便于理解!!!Private Sub Command1_Click()Dim n As Integern = jiecheng(3)Print nEnd SubPublic Function jiecheng(p As Integer)Dim sum, i A...
回复

使用道具 举报

千问 | 2009-2-19 12:52:34 | 显示全部楼层
Private Sub Command1_Click()Dim n As Integern = jiecheng(3)Debug.Print nEnd SubPublic Function jiecheng(ByVal p As Integer) As LongDim i As Integerjiecheng = 1F...
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行