Friday, March 20, 2009

Function Returns Multiple Values?

Hello friends... this is the question oftenly asked in Interviews...

Returning a multiple values with function....

Function calc(a,b,c,d) ' Called Function ----- Function Definition

msgbox "a is value "&a ' a value passed from calling function
msgbox "b is value "&b ' b value passed from calling function

c=a+b ' Calculation of c
d=a-b ' Calculation of d

calc=array(c,d) ' Assigning Calculated values in array of Function name (calc here)

End Function


res=calc(10,20,c,d) 'Calling Function with Passing input parameters
msgbox res(0) ' returning a value from array of Function (calc here)



' User Inputs to perform Addition and Subtration and returning both the values by using Functions... (returning two values by using single name of Function)


Function calc(a,b,c,d)

msgbox "Value of a is "&a
msgbox "Value of b is "&b

c=cint(a)+cint(b)
d=cint(a)-cint(b)

calc=array(c,d)

End Function

a=inputbox("Enter a value:","a Value",10)
b=inputbox("Enter b value:","b Value",20)

result=calc(a,b,c,d)

msgbox "No. of Values in Array is :"&ubound(result)+1

iter=ubound(result)

For i= 0 to iter

msgbox "Returned Value of "&i+1&"th iteration is "&result(i),3,"Returned Value of Function"

Next




Thanks & Regards
Baba Fakruddin.D

No comments:

There was an error in this gadget