Monday, December 10, 2012

PROCEDURES AND FUNCTIONS in visual basic


Dim a, b As Integer
Private Sub CmdTable_Click()
List1.Clear
table (Val(Text1.Text))
End Sub
Public Sub table(ByVal t As Integer)
Dim i As Integer
For i = 1 To 10
List1.AddItem i * t
Next
Text1.Text = ""
Text1.SetFocus
End Sub
Private Sub CmdFact_Click()
fact (Val(Text4.Text))
End Sub
Public Function fact(ByVal num As Integer)
Dim i, f As Long
f = 1
For i = 1 To num
f = f * i
Next
fact = f
MsgBox "Factorial is: " & fact
Text4.Text = ""
Text4.SetFocus
End Function

Private Sub Cmdswap_Click()
x = Val(Text2.Text)
y = Val(Text3.Text)
MsgBox "Before calling Sub Procedure x= " & x & " and y= " & y
swap x, y
MsgBox "After calling Sub Procedure x= " & x & " and y= " & y
End Sub
Sub swap(ByVal x As Integer, ByVal y As Integer)
Dim z As Integer
z = x
x = y
y = z
MsgBox "In Sub Procedure x= " & x & " and y= " & y
End Sub

Private Sub CmdSwap1_Click()
a = Val(Text5.Text)
b = Val(Text6.Text)
MsgBox "Before calling Sub Procedure= " & a & " and b= " & b
swap1 a, b
MsgBox "After calling Sub Procedure= " & a & " and b= " & b
End Sub
Sub swap1(ByRef a As Integer, ByRef b As Integer)
Dim c As Integer
c = a
a = b
b = c
MsgBox "In Sub Procedure a= " & a & " and b= " & b
End Sub
Private Sub CmdExit_Click()
End
End Sub




 Output


Note:- Please set some property of your control like caption or id corresponding to your control
Goto Visual Basic Programs Index Page