0% found this document useful (0 votes)
10 views21 pages

Unit-IV Visual Basic

Uploaded by

vanshthakral2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views21 pages

Unit-IV Visual Basic

Uploaded by

vanshthakral2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

• Procedure and FUnctions-

. . -
e is a Waste Of time
In applications where some part of the code gets·rep. eated, thes
'

• '

coding for the same


and effort that can be used more productiv.ely because we ar~ .
( •
.
we need develop
to
tasfover and over again. Since the Same c~e ·is to be reused,
'
and_ is used as when
some method by which repetitive code is written only once
ramming languages
required. Therefore, concept of procedures is used in most prog
. to enhance re-usability.

nts that performs a


A Procedure is a named unit of a group of program stateme
well-defined task.

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.

_ • (iv) A change can be made once in th •d


• ram.
prog e Proce ure rather than multiple times in our
/ -
The procedure in Visual Basic are of two types as :
(i) Subroutines
(ii) Functions •

7J.1 Subroutines
'

A subroutine is a block of statement that carries out of a well-defin


ed task. A sub or
subroutine is~ ~lock of statements that is executed in respo
nse to an event.The
subroutines d<:>es not return any value. Its format is as
•-
Format

[Private/Public/Friend/Static] Subname [(arglist)]

[Statements]
'
[Exit Sub)

[Statements]


----------
End Sub

Where

{a) Public : Public keyword makes a procedure accessible to


all other proce-
dures in all modules and forms.

{b) Private: Private keyword make a procedure accessible


only to other proce-
dure in the module or form in which it is declared.

{c) Friend : Friend keyword is used only in class modules and


specifics that the
procedure is visible throughout the project, but not visible to
I
I .
a controller of an
instance of an object.

[211]
(d) Static . Static keyword specifies that procedure•s local va.
• nabtes
preserved between calls. . s~~ ~

(e) • Subname : It identifies the name of the procedure. .

(f) Arglist : It is list of variables representing argument that a .


• . . re Pass
procedure when it is called. You can separate multiple var- b 8(1 lo 1h
• ia les With ~
m~. '

All the event procedure in VB are coded as •subroutine'. The statern .


. • ent 1n
tine is executed. When the 'End Sub' statement is reached cont ~ 8Ubr0,.
• • , ro1 ret "'
calling program. It is possible to exit a subroutine prematurel y With 'E . urn to the
· x1t Sub• Stat8'
ment. - •

Example

Sub sq~ar~- (a As Integer)

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

... These are two ways of callin . •


V

·-~-·
..~ :~ ..
.. . 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 ; .

1,t.1.3 Event handler and Subroutines -, l

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. --,

Tutorial (Simple Interest)


~

in an
Write a procedure to calculate the simple interest and invoke this procedure
other procedure. \ · . .· ·

1~ Design a form as· shown in figure shown below :

-. Simple Interest j__ I_ □ Ji_~

Command!
name is
cmdSI

Command1 Name cmdSI

Caption •Calculate S.I.


,
cmdexit ·:,:
'
i ·command2 Name .
...,,;,1,f.
,,'
Caption E&xit. ........
\ .,.,

Form1 Caption Simple Interest I • o

•-~· ·!

[213] . ~·1·
\ .
'4
-~
...
. ..,......-,
'
2. Write the code for cmdlSI click event as :

Private sub cmdSl_click( )


• Dim P As Single, R As Single, T As Sing
le
P = lnputBox ("Enter Principal Amount")
R = lnputBox ("Enter Rate of Interest")

T = lnputBox ("Enter Time")


Call Slnterest (P, R, T)
End Sub
3. Write the procedure to calculate the simple
interest as :
.
Sub Slnterest (P As Single, R As Single, T
As Single)
Dim S As Single ~

\
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

Write a procedure that rec .


.. e1ves two b . . .
div1s1ble by second one num ers and pnnts whether the f1rst one 15
O
r not and in k .
• . vo e it to another procedure.
1. Design a form as show . .
n in figure shown below :

Command!
name is
cmdcd

2. Write the code for cmdcd click event as:

Private Dim N1 As Integer, ~2 As Integer

N1 = lnputBox ("Enter 1st Number")

N2 = lnputBox ("Enter 2nd Number")


-J•

Call· Divisible (N 1, N2)

End Sub

3. Write the procedure to check the divisibility

Sub Divisible (Num1 As Integer, Num2 As Integer)

If Num1 mod Num2 = 0 Then


.,.
Print "Divisible"

Else

Print "Not Divisible"

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-·

[Private I Public I Frien 18t .


d atic] FunctionName [(arglist)] [As type]
-------

[statements]

[functionname = e~pression]

[Exit Function]

[statements]

End Function (

Where

(a) Public : It makes a function accessible to all other procedures or functions in


all modules and forms.

(b) Private : It makes a function accessible only to other procedure or functions


in the module or form in which it is declared.
(c) Friend : It is used only in class modules and specifies that the functions is
visible throughout the project, but not visible to a controller if an instance of an
I
object. •
variable should be preserved be-
(d) . s local
Static : It specifies that the function' .
tween calls.
(e) Functionname: Name of function.
(f) Arglist : List of arguments that are passed to function when it is called.

(g) Statements : It is group of statements to be executed within function. .


[217]

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

Function Sum (num 1 As Integer, num2 As Integer) As Integ


er
If (num1 <0 OR num2 < O) Then

Exit Function

Else
Sum = num1 +· num2

Print usum is" ; Sum

End if

End Function

7.1.2.2 Calling Function

A function can be called by using its name in an expressio


n. Its format is ,
FORMAT ~

. Call Fµnctionname (arg1, arg2 --- argn)

OR

Functionname arg1, arg2 --- argn

Example •

Sub Getsum ( )

Call sum(12, 13)

End Sub

[218)
rutorial (Using function)

. Write a function to calculate the simple interest and


invoke this function in another
procedure.

1. Design a form as shown in figure shown below :

Iii Simple Interest _-~ ~ !®


,:::·:: .................................................... .
• • •• " • • • • • ••• t ••••••••••
•••••••••
• • • • • • • • • •• • • • " •••• ' ••••••••

.:::::
t ••••••••••


. . . :::::
. .. . ::::: ::::: ::::: ::::: ::::: :::

~

... •
. . . . . . . . . . .. . . . . . . . .. . . ... . .. . . ,
.........
• • • • ..

........ ........ ........ ........ ........ .. , ....... ...... .


.
• • • • • • • •

.
• • • .............

. . .........
. . . ..... ., .....
.. t •

. .....
• • •

:::• ..........
........ •.... . . .. . . .. .
..... ..... .. . ~

......... .......
.. . .. . . . . . ....... ..- ...... ......
. . .......

....... ....... ♦ ♦ ♦ ♦


t
♦ t
t

.....

♦ ♦
I •

I I I • ♦ ♦ t

. . . . ......
. . . ....
I t, t ••

. . . ...... . . .." .....


t • •

. . ......
I t I

Command!
name is
.. . ......
....... . . ......
,:
..... .....
...

...............· ..... ..........


.......... ..........
..... .~ Command2
name is

___
~ 1

cmdSI
' It " • • I &· I • • • • • • • 1 , •

............ , ..... , ....


• I • •

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 •

2. Write the code for cmdcd click event as: •

Dim P As Single, R As Single, T As Single, SI As Sing


le
P = lnputBox ("Enter Principal Amount")

R = lnputBox ("l=nter Rate of Interest")

T = lnputBox ("Enter Time")

SI = Slnterest (P, R, T)

Print "Simple Interest is" ; SI

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

4. Click event of cmdexit is


Private sub cmdexiLclick( )
End
End Sub
5. Run the program click on cmdSI command button, we get the _input box
the value of p, q and we get the following output on shown below : ' enter

11111 Simple Interest

7.1.3 Passing Parameters (or Arguments) to Procedui;e


I
I

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.

7.1.3.1 Call By Value (Pass By Value)


..

When an argument is passed by value, ·only.a copy of a variable is passed. This


means that even if the values passed are modified by the su.bro~tine, those modified
[220]
values remain local to that sub . • •-~.
· routine Wh ~
to the calling program the va . 'b • en the subroutin_e exits and return control •,.
. -' ~ na les tha • -
originally h~d before the call. In VB t t were pas~ed by val~e -~tai~ the value they
argument passed by value. ' he keyword 'By Val' is used to indicate an

example_ - .
• I

. .
Sub Fo~_Click () . '

Dim Var As Integer T •

Var=4

Debug.Print Var

.Twice (Var)

Debug.Print Var

End Sub

Function Twice (By val num As lnt~r)


num ~ 2 * num
(
Debug.Print num I .

i
~nd Function
• t ': ..
. .. .. ••

Output - ' • • •• • : - :, -1·


I •

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

value of the variable .'var' is retained. :-:


[221]

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

Function Twice (By Ref n~m


As Integer)
num =2 * num
Debug.Print num
End Function
Output
4
8 ·,
8
When variable are passed by
reference only one memory
function twice is called 'num' iocation is involved. ~ en
becomes the name of mem
· is doubled the value in ~e ory location when the value
mory location become a: Af
ter the completion of this
procedure the name 'num' is
forgotten and doubled value
lives on in 'Va~.
[222)
,1.3.3° passi~g an Unknown Number of Arguments (Param Arr
1 ay)
VB introduced 'Param Array' keyword which allowed you to pass
a variable numb8r
of arguments to a function (procedure).

example

Here an example adds a number of arguments to ListBox contr


ol as :

Function AddNamestolist _(ParamArray NameArray(1)) •


I

For Each X In NameArray • •I .;·


.I

List1. Addltem X I
I
I
' I

Next X

End Function

This Function's argum~nt is an array prefixed with keyword 'Para


m Array•.

7.1.3.4 Using Optional Arguments

VB allows you· to create subroutines or Functions that have opti.o


I - ~ •

nal! arguments.
Optional arguments that may or may not be passed; and if not pass
ed will assume
\

a default value. Optional arguments are specified in the


' \

procedure header with


keyword optional. All optional arguments must be placed at the end
..


of the argument ,I ,,,

list.

Optional arguments that are not passed will take default values. Num
eric arguments '
'

will default to o, Strings will default to


11 11
'I

Booleans will defa~lt to ~alse, etc.


,
.
;'

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 •
' '

1. Call Slnterest (5000)

In this case, P = 5000, A= 10, &T = 2.

2. Call Slnterest (5000, 18)

In this case, P = 5000, A = 18 and T .= 2.

3. Call Slnterest (5000, 18, 5)

In this case, P = 5000, A = 18 and T = 5.


s have
Optional arguments are useful in such cases where ~ome arguments alway
the same value. For example, Bank Interest may remain the same Jor all customers
, ,.

for a particular period of deposit.

Tutorial (Optional Arguments)


in an
Write a procedure to calculate the simple interest and invoke this procedure
~.
other procedure.

1. Design a form as shown in figure shown below : .


I

l I
.
J
,

._ s·imple lnte,:est / ... XI


.
I

. . . . . . . . . . . . .. ... .. .. . . . .. . .. . . . .. . ..
.. .
..............
........... .i1 ..............

.• •• ,I • : .. • !.· • • • • • • .• • • •. -~ • ~ • • • ~ • • • .• • • •
• • • • • JI •

.....· . , ....... ....... ....... ....... ....... .. .


• • • ,' i • • • • •
• ,, • ~ • •' • • ' • • ! • .._ l •. •· • •· • •• t • • " • • ·•

....... .
............

···
••• ,

······
.......... , .• 11o

······. .······ .
•••• .-

.,····•
• f
·'·•··.······
. . . . . ......
.... ,,. . ......• • •
. . ........
. .. . ., .......
. . . ., .... • •
. . . . . ..
. . . . . .....
• •. • • ., • • t . • . . • . • . • . • • . . • . . . . .. . . .

.~,, ....... ... . ..


... , ....... ...... ..... .. .. ......
. . . . ...... . ., ..... ..
.....
. . . . . ......
. . . . . . . ......
,
~ · ,.

•.
.,, .... -·· ................ "
~

, ·-• _.
•,• .•
Commandl • • ·• •". ~ • •• • • •• •• - •• • ~ • • - •• • • • , . . • • • ·• , ..... • • • . . • • • • •
Command2
• • • • • • • • • • • • •
• .. .. • • • • • • • • • •

~ name is
• • • 'Iii , • • •

name is • .• .•.~.•. .• .•.•.~. .• ... .•.•. .• .•.•.•. .• .• f•'•. ,• .• .•. •. .•.•. .•.•. .•.•. •. .•. •. .•.•. .• •.
~

• " • -- • .-·• . •... ·• • • •-· •...... • • • • • .•......


• • • • • ...... •. ., . . . .. .- .. .. . . cmdexit
cmdSI ... ..... ...... _ •

..:::
: : : : ~ exit· • .......
cu,;rte s.i: ·, 1:.· :...: :' :...
.. ,. .. ..•• ••~f -. . . :! •
)

~ •
■• • ·• • • I • • • '. •, • • • • • • .. , • • .. - " • ·-.. • • • •
• "I ,i • .. •

...... ...... ...... ...... ...... ... -- ..... . 1 'I • • • • ._. • • • • I f • • II • r • • •


e r • ' t t f ... • _. " " • • • •

[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 :

... Simple Interest r:- 1


'.:. !_X-
.... ......... ~--····--····•,
••ot•·····"··· ··i•·········•·"·"········
,.

., ................. ................········· ....... .


' . . . .... ... . . ... ·~ .-. , . .. . ..
. . . . . .. . .. ..•••··••··••···••1
..... ·........ "' ................... ........ ~···
•ttA•ttlt'l<•t• .. ••·•··••lt1

,. ................. ................. ........ .


••••••A••••••• c•••••••••••r•• •••••••••

.......... ... , .......... ,., ................... .


• • • • f • ~ • •
)
t • • • • t • • .. • • • .. • • • •· • • • • • • •• • • • • • ..

....... ..., .........


............ , ................. ., ..... .
••#-••••~••••f• •·•••••'••••••••r ••••••••••
............. ......... , ............. .
.,
.... ,., ...... ..... ........... .... , ......... , .
~

.,
,
Commandl ............... .............. ~.~, ................ ._. ,
\
, ;,. Command2
name is name Is
cmdSI . cmdexit
: ..... , 'ca1cu1at;s.1. ,, [:::::::: ::.< ~1t·" · · •:::
...,.~··-~--
'I: •


..................········-~

..


• •
• •


·····..
......... ..............
.. ..

• •

.. •
• ..


..


• '•

• •

l
.. ,

• • •
..
,.


• •
• ,...


"


• ,.


,.
.. •
• "




• •
• '



.. •
.. • "' • •

• • •

2. Write the procedure to calculate the simple interest as


Sub Slnterest (P as Single, R as Single, T as Single) •
Dim S As Single
S = P * R * T/100 ••
Print
Print "Simple Interest is 11 & S
End Sub
• 3. Write the code for cmdSI click event as :
Private _sub cmdSl_click( )
Dim p As Single, A As Single, T As Single
'Using Named Argument' I

=
Call Slnterest (P : 1000, A : 1o, T : 2) = =
End Sub •
4. Click event of cmdexit is

Private sub cmdexit_click( )


End

End Sub

5. Run the program and


we get the following output :.

7.2 Passing an Array to aProcedure (or a function)


We can pass an array as an argument to a procedure or a function. Let us understand
this concept using the following tutorial.

Tutorial (Passing an Array)

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


._

..


.................. " ......................... .


... ~ ..... , . . . . . . . . . . . . . ~ • · • · i . . · .. · · · · , · · · · · · · ·

.. " ~ .. ) . .. " .. . . . . . . . ,, . . . . . . . . . . . .. . . . ' . .. "

Commandl Command2
name is
name is
cmdexit
cmdpa
' ' Pass an Array, I- · ·· · ' · · ' · Exit • .

•~ : l ~-: : : : : :. : '. : ;, : ; ~ i ; :ii i-i i : : : : : : : i i ~ ; i·; .

We shall place code in the cmdpa command button to pass an array to a


procedure we shall write.
• [ 227]
2. Write the code for cmdpa first as :
-Private sub cmdpa_click()
Dim a(2) As Single
a(O) = 1
a(1) = 3
a(2) = 5
Call the procedure and pass the Array
Call takeArray(a)
End Sub
In this code, we have declared a static array of 3 elements. The next three
lines of code assign values to each of the three elements of the array. Finally
we have called the procedure 'takeArray' and passed it a single argument,
. I
the array 'a'.
3. Write the SubProcedure to accept the Array as an argument
Private Sub takeArray(x () As Single) •
Dim element As Variant
For each element In x .
Print element •
Next
End Sub
- . .
In this code, we have used an array as argument, After that, we have written
the code to print the value of each element of array on the form.
4. Click event of cmdexit is
Private sub cmdexit_click()
End
End Sub
5. Run the program and we get the following output : ,· . , ••• .

[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

Tutorial (Function Returnin


_____ 9 Arr~y)
1. Design a form as show b
n elow:

._ Fcrrm1 • .
!llil£1

' l

Commandl
name is Command2
name is
cmdra
cn,dexit

• I

2. Write .the Function to pass an Array back to the calling procedure as :


. .
Private Function RArray() As Variant •· • ••

Dim a(2) As Single

a(O) = 1

a(1) = 3

a(2) = 5

Array as a Retu_rn Value

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)

You might also like