Unit-IV Visual Basic
Unit-IV Visual Basic
. . -
e is a Waste Of time
In applications where some part of the code gets·rep. eated, thes
'
• '
Advantages of Procedure
(i) Th
the same code
ey save you programming time by allowing you to reuse
over and over in your program.
(ii) Th .
andable pieces.
ey allow you to break up large or compleX tasks into underst
(Ui) I .
t makes our code easier to read and to maintain.
7J.1 Subroutines
'
[Statements]
'
[Exit Sub)
[Statements]
•
----------
End Sub
Where
[211]
(d) Static . Static keyword specifies that procedure•s local va.
• nabtes
preserved between calls. . s~~ ~
Example
Dim s As Integer
s=a*a.
.
.
MSgbox ("Square of .the nUmber
....__
,s· &_ str(s))
.End Sub ...
.....,·' :..
I
~
•I
• This procedt.ire is used to find th8 square of a numb
•
\"
' '
. e~
.
7•1•1•1 Calling Subroutine
•• I
,
!+
;i
·-~-·
..~ :~ ..
.. . ut1ne as:
. ,.!'
.....
~ ,.. ;
• Call Subname
.,
I • •
; ... rgn) ; . '
'· \
Subname ,. g ,.--r--,, _ \•
I
I
I
7.1.t.Z Exit Sub Statement ,,
.. . .
.. :
dure.
The 'Exit Sub' is keyword Which cause an immediate exit from a 'sub' proce ; .
program
In VB, Event handler and subroutine are· same. An event handler is a short
event
segment of eocle that is executed each time an external condition trigger the
This
when the user clicks a control, the control's click event handler executes.
perform
handler is nothing, but a subroutine, which perform actions you want to
when the control is clicked. --,
in an
Write a procedure to calculate the simple interest and invoke this procedure
other procedure. \ · . .· ·
Command!
name is
cmdSI
•-~· ·!
[213] . ~·1·
\ .
'4
-~
...
. ..,......-,
'
2. Write the code for cmdlSI click event as :
\
S = P * R * T/100
Print "Simple Interest is' & S
End Sub
4. Click event of cmdexit is
Private sub cmdexit_click()
End-
End Sub
5. Run the program click on cmdSI command butt
on, we get the input box, enter
the value of p, qand r and we get the followin
g output on shown below :
111 Simpl e lnte,-est
. '
:·.·1·t
i
( [ 214] I
!
Tutorial
Command!
name is
cmdcd
End Sub
Else
End If
End Sub
[215]
b
4. Click event of cmdexlt is ...
Private sub cmdexlt_click()
End
I. .
End Sub •• - •
. ~ ommand button, we get the input box
enter
5. Run the program click on cmd N2c(saY 5) and we get the following ou~
nd - . lit on
the value of N1 (say 45) a
shown belo~ :
f orw1
..
7.1.2 Functions
a pair of
The function is a procedure i~ whcih a ~lock, of ~tatement a~e placed in
but differ
'Fun~tion/End Function' statements_. Functions are similar to subroutines
program.
from subroutine as function perfrom calculations and retum result to calling
• There are two varieties of functions : •
(i) User-defined. functions that you cr~ate. - •
Built-in funcitons or library functions that are already defined (programme
d)
(ii)
and are a part ot· Visual Basic. Th_~s~_ fun~ic~>ns can be directly used.
The value being returned by_the function is assigned to the function name
, which
return
a~tomatically returns it to ~he calling procedure or function. A function may
one value only. . ·_ ·• •
I
-- -- -- -- -- -- -- -- -- -- -- -- -- - -- '
. .I .
,• .
-~ - Nole
• •
1
I Th~ mSin difference betvJeen subroutine and function is ihat a subroutine 1
,___________ -- --
I •does not return a value whereas a function always· return- one, value.
l .
.-/
. ~
• • - - - - - - - - - - - - - [216]
You can call and pass argum •
defined function is : ents to function similar to subroutines. Syntax of user-·
[statements]
[functionname = e~pression]
[Exit Function]
[statements]
End Function (
Where
Ill-._ I , t, :.
7.1.2.1 Return Value
While argument solve the problem of passing value to func
. tion, we ha
out a method to pass value back from a function 11· Ve to f'1
to ca mg.program. This •
by using return value. we also require the result of a function is ach·19lJr~
- for further c 1 ev8~
acu1arions
.
Example
Write a function to find the sum of two positive numbers
Exit Function
Else
Sum = num1 +· num2
End if
End Function
OR
Example •
Sub Getsum ( )
End Sub
[218)
rutorial (Using function)
.:::::
t ••••••••••
•
. . . :::::
. .. . ::::: ::::: ::::: ::::: ::::: :::
•
~
... •
. . . . . . . . . . .. . . . . . . . .. . . ... . .. . . ,
.........
• • • • ..
.
• • • • • • • •
.
• • • .............
. . .........
. . . ..... ., .....
.. t •
. .....
• • •
:::• ..........
........ •.... . . .. . . .. .
..... ..... .. . ~
......... .......
.. . .. . . . . . ....... ..- ...... ......
. . .......
♦
....... ....... ♦ ♦ ♦ ♦
•
♦
t
♦ t
t
.....
•
♦ ♦
I •
♦
I I I • ♦ ♦ t
. . . . ......
. . . ....
I t, t ••
. . ......
I t I
Command!
name is
.. . ......
....... . . ......
,:
..... .....
...
___
~ 1
cmdSI
' It " • • I &· I • • • • • • • 1 , •
cmdexit
• • • • I • • I I f • • I It
" •· • • • • • • • • I • • • I • • • I • • • I • I • • e • e • • • • I' •
..
. Calculate S.I. _,,
• (__ : : : : : : : :
....
··••\••··----- ••
. . . . . . . . ... " .........................
•
t'
•
•
•
•
•
I
•
I
•
I
.,
•
•
I
• I
. • • , • • • • • I • • I • • • • • I I I • I Ill> I • • I I
• ._ • • • I • • • f It • ,t I t I I I • • • • I I •• • e I •
SI = Slnterest (P, R, T)
End sub
· 3. Write the function to calculate the simple interest as
:
Function Sinterest (P As Single, R As Single, T As Sing
le) As Single
Dim S As Single
. .
S = (P * R * T)/100
Sinterest = S
End Function
., -
[219]
l t
Code in a function needs some information about stat~ of the program to do its job.
This information consist of variables passed to the procedure when it is called.
·"When a variable is passed to a function or subroutine•, it is called an 'Argument' or
parameter. . '
Parameter(serve as placeholder for the data we need to pass into the procedure
(Functions or Subroutines). Naming of parameter is similar to naming of variables.
You can use two different methods for passing in parameters to a procedure : call
by value and call by reference.
.
Note that
•
the parameters
I
that appear
.
in the definition •
of a procedure are called formal parameters and the parameters that appear in the
procedure call statement are called actual parameters.
example_ - .
• I
. .
Sub Fo~_Click () . '
Var=4
Debug.Print Var
.Twice (Var)
Debug.Print Var
End Sub
i
~nd Function
• t ': ..
. .. .. ••
The Variable var is passed onto the argument num by value-~d Output will be as :
..... ,., ~
4 \
'\ '
8 \
I!
'I
4 ' ,)
''
. ' \' I
·•\T~o memory location are used, nur:n and var.. ~e .~alue of var is copiedt onto num.
l l ' • ; • • : • ., • • ,, l., - •• •• ~
When num is. doubled, the value of num become 8. After the completion of the
. ·- ,.· • .,; ,- •• • • ~ r • •
. I
I
I
I
· ·, function (procedure) the mem~ry lo?ation o! num disappears and so the original '
I
I
" I
7.1.3.2 P ~ in g Argument
'By Rer
. . nee ly memory addres
When vanables are passed by refere , on__ . . s of the variabtes
• I es passed-can be mochfied by the subrolJti
are passed. This means ~~ th ed U
after ~e subroutine exits an ~
and would have the new value d returns control to the
. pected to contain a "re
calling program. A variable th turn• value or outp
value must be passed by ref at is exTh e keyword 'By Ref is used to ind
erence. icate :
argument passed by referenc
e. •
Argument are by default pass
ed by reference in VB.
Example
Sub Form_click ( )
Dim Var As Integer
/ Var=4
Debug.Print Var
Twice (var)
Debug.Print Var
End Sub
example
List1. Addltem X I
I
I
' I
Next X
End Function
nal! arguments.
Optional arguments that may or may not be passed; and if not pass
ed will assume
\
list.
Optional arguments that are not passed will take default values. Num
eric arguments '
'
You can also explicitly set the value of an Optional argument by ,C'
~ssigning it a value (
'
~
in the procedure header.· ~
I
c
[223]
@Ji
·, . [:]
'
Example
Consider the following procedure header
1 ,. <)
Sub Sinterest (P As Single, optional A As Si~gle = O, optional T As Single
Now consider the following calls to. Slnterest •
' '
l I
.
J
,
. . . . . . . . . . . . .. ... .. .. . . . .. . .. . . . .. . ..
.. .
..............
........... .i1 ..............
.• •• ,I • : .. • !.· • • • • • • .• • • •. -~ • ~ • • • ~ • • • .• • • •
• • • • • JI •
....... .
............
···
••• ,
······
.......... , .• 11o
······. .······ .
•••• .-
.,····•
• f
·'·•··.······
. . . . . ......
.... ,,. . ......• • •
. . ........
. .. . ., .......
. . . ., .... • •
. . . . . ..
. . . . . .....
• •. • • ., • • t . • . . • . • . • . • • . . • . . . . .. . . .
•.
.,, .... -·· ................ "
~
, ·-• _.
•,• .•
Commandl • • ·• •". ~ • •• • • •• •• - •• • ~ • • - •• • • • , . . • • • ·• , ..... • • • . . • • • • •
Command2
• • • • • • • • • • • • •
• .. .. • • • • • • • • • •
•
~ name is
• • • 'Iii , • • •
name is • .• .•.~.•. .• .•.•.~. .• ... .•.•. .• .•.•.•. .• .• f•'•. ,• .• .•. •. .•.•. .•.•. .•.•. •. .•. •. .•.•. .• •.
~
..:::
: : : : ~ exit· • .......
cu,;rte s.i: ·, 1:.· :...: :' :...
.. ,. .. ..•• ••~f -. . . :! •
)
~ •
■• • ·• • • I • • • '. •, • • • • • • .. , • • .. - " • ·-.. • • • •
• "I ,i • .. •
[2241
2. Write the code for cmdcd cl· k
1c event as:
Sub Slnterest (P as s· 1 . .
2) ing e, Optional R as Single = 1o, Optional T as Single =
Dim S As Single
S = P * R * T/100
I •
Print
Print "Simple Interest is .. & S
End Sub
3• Write the function to cal~ulate the simple interest as :
Private sub cmdSl_click( )
Dim p As Single, R As Single, T As Single
P = lnputBox ("Enter. Principal Amount•)
R = lnputBox ('.'Enter Rate of lnteresr)
T = lnputBox ("Enter Time")
Call Slnterest (P, R)
Call Slnterest (P, R, T)
End Sub
4. , Click event of cmdexit is
Pr:vate sub cmdexit_click( )
End
End Sub
5. Run the program click on cmdSI command button, we get the input box, enter
the value of P (say 1000), R (say 15) and T (say 3) and we get the following
output on shown below :
r ,,,,~ 1
7.t.3.5 Using Named Arguments
Using named arguments, the arguments to the pr~~edure canbe passed in any
order. All required arguments must be passed, but optional arguments can be omitte
if desired. Using named arguments saves coding when calling a procedure that h d
. as
a lot of optional arguments but you only need to pass a few of them.
The following example demonstrates the use of the _named arguments, when You
carJ specify the arguments to be passed in the calling statement using the syntax.
ArgumentName : = value
Tutorial (Using Named Arguement)
Write a procedure to calculate the simple interest and invoke this procedure in an
other procedure. I
1. Design a form as shown in figure s~oVJn below :
.,
,
Commandl ............... .............. ~.~, ................ ._. ,
\
, ;,. Command2
name is name Is
cmdSI . cmdexit
: ..... , 'ca1cu1at;s.1. ,, [:::::::: ::.< ~1t·" · · •:::
...,.~··-~--
'I: •
•
..................········-~
•
..
•
•
• •
• •
•
·····..
......... ..............
.. ..
• •
•
.. •
• ..
•
..
•
• '•
• •
•
l
.. ,
• • •
..
,.
•
•
•
• •
• ,...
•
"
•
• ,.
•
,.
.. •
• "
•
•
•
•
•
•
• •
• '
•
•
•
•
.. •
.. • "' • •
• • •
=
Call Slnterest (P : 1000, A : 1o, T : 2) = =
End Sub •
4. Click event of cmdexit is
End Sub
Write a procedure to calculate the simple interest and invoke this procedure in an
other procedure.
1. Design a form as shown below :
. . .. . . .. . . . . .. . . . . . . . . . . . . . . . . . . . . . . .
•
•
•
,
..
~
•
•
t-
•
•
•
•
•
•
•
•
..
•
•
•
•
•
•
•
•
..
•
..
•
..
•
•
•
•
'
•
•
•
•
'
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
t
•
._
..
•
•
•
Commandl Command2
name is
name is
cmdexit
cmdpa
' ' Pass an Array, I- · ·· · ' · · ' · Exit • .
[228]
1,.,1 functions Return~ ....g Arrays .
&•UJ• ,.
~ A function
. can retum an arr . .
-
ay• Let us co • d
this concept. nsider the following tutorial to understan
._ Fcrrm1 • .
!llil£1
' l
Commandl
name is Command2
name is
cmdra
cn,dexit
• I
a(O) = 1
a(1) = 3
a(2) = 5
RArray = a
End Function
In this code, ·we have assigned the array a to the name of the function which
results in the array 'a' being returned to the calling procedure.
[229]
1111111.
3. Write the code to rece
ive the Array as
Private Sub cmdra_C
lick()
Dim retumval As Var
iant
Dim element
'Assign the return valu
e to a Variant Variable ••
retumval = Rarray
For Each element in
returnval l
l
I
Print element
Next
End Sub
In this code, we have
declared a variable to
function. This next lin hold the return value
e of code is the call from the
returns a value, we to the fu~ction. Since
have assigned it to th a function
variable retumval no e variable returnval.
w has the characteris Note that the
el~ments of array us tics of an Array. Finally, print all
ing For Each loop.
4. Click event of cmdexi
t is
Private sub cmdexit_
click( )
End
End Sub
5. Run the program and
we get the following
output :
111t form1
[230)