VB问题,有关于fun过程的,求助

VB问题,有关于fun过程的,求助,第1张

关键在这里Text1

=

Str(fun(x)

+

fun(x)

+

fun(x))

你的x初值是2,而你的函数定义fun(ByRef

n

As

Integer)是按照地址传递参数的,在这个函数过程中n的值是变化的,函数过程你执行了n=nn或n=n+n后这个传递的参数n的值就发生了变化,即你调用一次函数,参数的值就发生了变化,第一次调用时候n=2,执行函数后函数值为4,n=4,那么第二次调用函数时n=4,执行后函数值为16,n的值也是16,Text1

=

Str(fun(x)

+

fun(x)

+

fun(x))实际上变成Text1

=

Str(fun(2)

+

fun(4)

+

fun(16))

=

Str(4+

16+

256)=276

如果你定义函数参数是按值传递的就不一样了,比如fun(ByVal

n

As

Integer),这么定义后Text1

=

Str(fun(x)

+

fun(x)

+

fun(x))=Str(fun(2)

+

fun(2)

+

fun(2))

=12

#include<stdioh> int fun(); void main(void) { int i,n,a[500]; n = fun(a); for(i = 0; i < n; i++) printf("%d ",a[i]); } int fun (int a[]) { int i = 7, j = 0; for(; i <= 1000; i++) if (i % 7 == 0 || i % 11 == 0) if ( !(i % 77 == 0 && i % 11 == 0) ) a[j++] = i; return j ; }

Function sum(n) As Double

sum = 0

For i = 1 To n

If i Mod 2 = 0 Then sum = sum + i i

Next

End Function

楼主

VB

c

混了

看看这个

Function

fun(ByVal

n

As

Integer)

As

Single

Dim

i,

t

As

Integer

Dim

s

As

Single

t

=

1

For

i

=

1

To

n

t

=

t

i

s

=

s

+

1

/

t

Next

i

fun

=

s

End

Function

注意,调用

n不能太大了,否则

就得

dim

t

as

long

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/12181219.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-21
下一篇 2023-05-21

发表评论

登录后才能评论

评论列表(0条)

保存