LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

I need to pass function name as a parameter for a function.

I have two functions.

lib_function(int x, int y, ???(my_function));

my_function(int a, int b);

I want to call lib_function and lib_function calls my_function.
I can keep my_function parameter fixed. I want to give user to have any function name(i.e. my_function) and pass that name to my library (lib_function).
How do I do this?
Thanks for your help.
Thanks.
CVI 2010
LabVIEW 2011 SP1
Vision Builder AI 2011 SP1
0 Kudos
Message 1 of 3
(2,915 Views)
Don't try to pass the function name, pass a pointer to the function. To make it easy, all functions to be called by pointer should have the same return type and the same number and type of parameters. It just gets more complicated if this isn't true.
Here's a skeleton of the code. See the attached project for a working sample.
// declare a global function
int FunctionOne(int someNumber);
// declare a function to call a function
int FunctionToCallFunction(int (*fn_pointer) (int someNumber), int functionParameter);
...
// declare a pointer to a function
int (*fn_pointer) (int someNumber);
// set the pointer to the selected function
fn_pointer = FunctionOne;
// call the function to call a function
FunctionToCallFunction(fn_pointer, functionParameter)
;
...
}
int FunctionToCallFunction(int (*fn_pointer) (int someNumber), int functionParameter)
{
int functionReturn=0;
if (fn_pointer != NULL)
// call the function by pointer
functionReturn = fn_pointer(someNumber);
return functionReturn*100;
}
Message 2 of 3
(2,915 Views)
This is exactly what I was looking for.
Thanks for your help.

Sheetal
Thanks.
CVI 2010
LabVIEW 2011 SP1
Vision Builder AI 2011 SP1
0 Kudos
Message 3 of 3
(2,915 Views)