This presentation is the property of its rightful owner.
Sponsored Links
1 / 26

第六章 过程 PowerPoint PPT Presentation


  • 97 Views
  • Uploaded on
  • Presentation posted in: General

第六章 过程. Function 过程的定义与调用 Sub 过程的定义与调用 参数传递 可选参数和可变参数 递归. 在 VB 中,系统提供了常用的内部函数过程,如 Sin 函数、 Abs 函数,系统也预定义了一些事件过程,如 Click( )、Load( ) 等。. 除此之外,用户可以自定义过程: 以 Sub 保留字开始的子过程 以 Function 保留字开始的函数过程. 函数过程的定义和调用. 自定义函数过程有两种方法 第一:利用 “ 工具 ” 菜单下的 “ 添加过程 ” 命令定义,生成一个函数的框架 第二:利用代码窗口直接定义.

Download Presentation

第六章 过程

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


3892328

Function

Sub


3892328

VBSinAbsClick( )Load( )

Sub

Function


3892328

,


Function

Function

Function

[Static][Public|Private]Function ([<>])[As<>]

<1>

[=]

[Exit Function]

<2>

[=]

End Function


3892328

1.

2.:

[ByVal|ByRef][( )][As ]

3.ByValByRef


3892328

4. =

5.[Exit Function]IfSelect Case

6.[Public|Private]:Public

7.As<>Variant


3892328

Function Fact(n As Integer) As Long

Dim i As Integer, f As Long

f = 1

For i = 1 To n

f = f * i

Next i

Fact = f

End Function

6.1 N!Function


3892328

([])

1.

2.


3892328

6.2

Private Sub Command1_Click()

Dim x As Integer

x = Val(Text1)

Label2.Caption = Trim(Str(x)) & ""

Text2 = Trim(Str(Fact(x)))

End Sub


3892328

Sub

Sub

VBSub


3892328

[Public|Private][Static] Sub ([])

[Exit Sub]

End Sub

[]

Call [()]


3892328

6.3 N!Sub

Private Sub Fact(n As Integer,f As Long)

Dim i As Integer

f = 1

For i = 1 To n

f = f * i

Next i

End Sub

Call fact(x,y)

Fact x,y


3892328

1.

2.;

3.


3892328

1.

ByVal


3892328

6.4

Sub Swap1(ByVal x%, ByVal y%)

t% = x: x = y: y = t

End Sub

Sub Swap2(x%, y%)

t% = x: x = y: y = t

End Sub

Private Sub Command1_Click()

a% = 10: b% = 20: Swap1 a, b '

Print "A1="; a, "B1="; b

a = 10: b = 20: Swap2 a, b '

Print "A2="; a, "B2="; b

End Sub


3892328

  • Lbound()Ubound()

2.


3892328

6.5 tim

Function tim(a() As Integer)

Dim t# i%

t = 1

For i = Lbound(a) To Ubound(a)

t = t * a(i)

Next i

tim = t

End Function

Sub Command1_Click()

Dim a%(1 To 5)b%(3 To 8)

t1# = tim(a())

t2# = tim(b())

Print t1 t2

End Sub


3892328

  • 1.

  • 2


3892328

3.


3892328

vb6.0

optionalIsmissing


3892328

6.6 sub

Private Sub mul(first As Integer, sec As Integer, Optional third)

result = first * sec

If Not IsMissing(third) Then

result = result * third

End If

Print result

End Sub

Private Sub Command1_Click()

Call mul(3, 5):Call mul(2, 3, 5)

End Sub


3892328

Sub ParamArray ) )

Private Sub sum(ParamArray num())

s = 0

For Each x In num

s = s + x

Next x

Print s

End Sub

vb6.0

Call sum(1,2,3,4,5)

Call sum(1,2,3,4,5,6,7,8,9)


3892328

Visual BasicSubFunctionSubFunction


6 7 fac n n

6.7fac(n)=n!

Private Function fac(n As Integer) As Integer

If n = 1 Then

fac = 1

Else

fac = n * fac(n - 1)

End If

End Function

Private Sub Form_Click()

Print fac(4)=fac(4)

End Sub


3892328


3892328


  • Login