Passing a function as a parameter

S

Scott

Hi,

I was wondering if it is possible to pass a function as a parameter.
(If you have a link and/or an example, that would be great.)

My intention is to write a C/C++ like qsort function, where I can pass
a reference to an array and the comparison function with it returning
the sorted array.

Thanks,
Scott
 
T

Tom Ogilvy

the addressof operator probably is what you want.

the description in vba help:

A unary operator that causes the address of the procedure it precedes to be
passed to an API procedure that expects a function pointer at that position
in the -- argument list.
 
C

Chip Pearson

Tom Ogilvy said:
the addressof operator probably is what you want.

It isn't clear whether the original poster is writing the sort code in
VB/VBA or in C/C++. If the code is written in C/C++, then the AddressOf
operator will be useful. However, if the code is written in VB/VBA, the
AddressOf operator will be useless. You can surely use it to obtain the
address of a function, but there is no way in VB/VBA to execute the function
pointed to by the value returned by AddressOf.

I have a qsort procedure for arrays of simple variable types (e.g., Longs,
Strings, etc) at www.cpearson.com/excel/QSort.htm and a procedure for
arrays of objects of any type at
www.cpearson.com/excel/SortingArraysOfObjects.htm . Both support
user-defined comparison functions, but the functions must be named
"QSortCompare" (for arrays of simple variables) or "QSortObjectCompare" (for
arrays of Objects).

The only other way to do it would be to put the comparison function(s) in a
Class module and use CallByName to call the appropriate comparison
procedure, but this would likely introduce an excessive amount of overhead
if the arrays are large.

I use AddressOf in VBA in a Windows Timer Manager program I'm writing, but
all I do with the address is pass it forward to the SetTimer API function.
If you are staying within VB/VBA, there is nothing useful you can do with
AddressOf.

--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
(email address is on the web site)
 
S

Scott

Thanks for the responses.

Your sorting routines sound like what I was looking for Chip. I will
admit I was trying to figure out how to get the AddressOf to work in
VBA even though the references said it wouldn't. The other schemes for
passing functions did seem to have a lot of overhead for something that
would be frequently called for most sorts.

Some day I'll delve into the API world, but not yet.

Thanks,
Scott
 
G

Guest

The following works for me:

Sub PassFunctionAsParameter()

dim strFunctionName as string
Dim FunctionArg1 as variant (or whatever)
Dim FunctionArg2 as variant (or whatever)
Dim Result as double (or whatever datatype)

strFunctionName = "MyFunction"

Result = Run(strFunctionName, FunctionArg1, FunctionArg2)

End sub

Function MyFunction(FunctionArg1 as variant, FunctionArg2 as variant) as
double 'or whatever datatype you want

' do stuff

MyFunction = whateverstuffyouwanthere

End function


I've found this extremely useful where I store function names in an array,
and then call the functions when needed. If different functions have
different number of arguments, then just add optional arguments to each
function so that they all have the same number of required + optional
arguments. Make sure you test for empty optional arguments.


This may or may not solve the problem you had.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top