Returning multiple values from a function...

  • Thread starter Thread starter Brad Pears
  • Start date Start date
B

Brad Pears

If a function obtains two values you want to use, how do you return them to
the calling procedure??

In teh following sample code, I have only returned ONE value as show....

sub sub1
itemNo = somevalue
strDesc = GetDesc(itemNo)
end sub

function GetDesc(ItemNo as integer)
dim RS as recordset
dim strSQL as string
strSQL = "select Description , Price from Items where ItemNo = " &
ItemNo

' Obtain description and price
set RS = curentdb.openrecordset(strSQL)
' Return Description
GetDesc = strDescription
end function

The above example shows the returning of the description only. But what if I
want to also return price to the calling procedure? How do I do that simply?

Thanks,

Brad
 
The purpose of a function is to return ONLY one piece of information. You
COULD create a subroutine that returns values through the parameter list.

Sub GetDescPrice(ItemNo as integer, Desc as String, Price as String)
....etc
End Sub

Since the you are passing the values ByRef by default, the subroutine can
change the values, which the calling sub can use:

sub sub1
itemNo = somevalue
GetDescPrice(itemNo, strDesc, strPrice)
msgbox (strDesc & "--" & strPrice)
end sub

However, it would be just as easy to create a second function that returns
the price. Granted, it would not be quite as efficient, but in most cases,
the difference will be unnoticable to the user.

I should also mention that you can get the same thing by using the built-in
DLookUp function:

strDesc = DLookUp("Description", "Item", "[ItemNo] = " & ItemNo)
strPrice = DLookUp("Price", "Item", "[ItemNo] = " & ItemNo)

--
--Roger Carlson
Access Database Samples: www.rogersaccesslibrary.com
Want answers to your Access questions in your Email?
Free subscription:
http://peach.ease.lsoft.com/scripts/wa.exe?SUBED1=ACCESS-L
 
Brad,

A function can only return a single value. It seems to me that you
would need to write another similar function GetPrice to handle the
price requirement separately.
 
You include variables to take the return values you need as extra
arguments to the Function. By default in Access, Function arguments
are passed "by reference", which means that if you change their values
in the Function they will be changed in the calling code, too. If you
want to avoid this behaviour, which is somewhat counter-intuitive to
those brought up on most classical programming languages, you have to
explicitily declare arguments as ByVal, and most people don't.

For example:

.....
strReturn = ManyArgs (intArg, blArg)
.....


Function ManyArgs (Item1 As Integer, Item2 As Boolean) As String

Item1 = 2
Item2 = True
ManyArgs = "This is the return value"

End Function


After the Function call, intArg will contain 2 and blArg will contain
True

If a function obtains two values you want to use, how do you return them to
the calling procedure??

In teh following sample code, I have only returned ONE value as show....

sub sub1
itemNo = somevalue
strDesc = GetDesc(itemNo)
end sub

function GetDesc(ItemNo as integer)
dim RS as recordset
dim strSQL as string
strSQL = "select Description , Price from Items where ItemNo = " &
ItemNo

' Obtain description and price
set RS = curentdb.openrecordset(strSQL)
' Return Description
GetDesc = strDescription
end function

The above example shows the returning of the description only. But what if I
want to also return price to the calling procedure? How do I do that simply?

Thanks,

Brad


Please respond to the Newsgroup, so that others may benefit from the exchange.
Peter R. Fletcher
 
Peter R. Fletcher said:
You include variables to take the return values you need as extra
arguments to the Function. By default in Access, Function arguments
are passed "by reference", which means that if you change their values
in the Function they will be changed in the calling code, too. If you
want to avoid this behaviour, which is somewhat counter-intuitive to
those brought up on most classical programming languages, you have to
explicitily declare arguments as ByVal, and most people don't.

I think your "classical programming languages" must be different from
mine. Most of the languages I cut my teeth on -- Fortran, PL/I,
COBOL -- passed arguments by reference. I think C was the first
language I ever ran into that passed them by value. Unless, of course,
I'm getting senile in my old age.
 
A function can return an array, a collection, a private type, or an object.

for example:

set cln = fncln(idx)

or

n = fncln(idx).field1

Unless you assign the return value to a local variable, the function
will be re-evaluated for each variable in the return value. This is
most useful when you just want to use a common function for a number
of return types:

n = fncln("a").encrypt
m = fncln("b").decrypt

but it can also be useful with a variety of cached data situations:

n = fncln("field1").int
s = fncln("field2").str

(david)
 
I did most of my "classical" programming in various Assemblers and C,
and never used PL/1 or Cobol. As I recall, Fortran, which was my first
language, normally passed by Reference, but at least one of the two
dialects (Prime and DEC) that I used most passed by Value - this may
have been a non-standard compiler default. Also from memory, BASICs
varied widely. I think Pascal (if you will excuse the expression!)
also passes by Value.

You're right, however, that my "most" was incorrect, since, if you
count lines of code written, Cobol is probably "most" on its own (!),
and adding standard Fortrans certainly gives Reference the majority.

I think your "classical programming languages" must be different from
mine. Most of the languages I cut my teeth on -- Fortran, PL/I,
COBOL -- passed arguments by reference. I think C was the first
language I ever ran into that passed them by value. Unless, of course,
I'm getting senile in my old age.


Please respond to the Newsgroup, so that others may benefit from the exchange.
Peter R. Fletcher
 
Back
Top