Click to See Complete Forum and Search --> : Passing Parameters


John Hagen
June 19th, 1999, 02:02 PM
I was wondering if there is any way to pass a parameter, containing the name of a function that I want to call, and then call that function from within another function.

For instance:

void MyFunction()
{
...
MyTimer(MyFunctionToCall);
}

BOOL MyTimer(???? CallThisFunction)
{
...
CallThisFunction();
...
return TRUE;
}

I have a funtion that is a timer waiting on a message from an RS232 port. The timer is used to determine if a response was returned within a given period of time. The result is then passed back as a TRUE or FALSE.

What I want to do is this: Send a message to the RS232 port and then call the MyTimer function. If the timer function times out (no response from the RS232) I'd like to be able to resend the original message (CallThisFunction) and wait again.

My problem is I want the timer function to be a generic use function, and to embed 30 or 40 calls to specific RS232 messages, in the timer function, is not desirable. So by sending the RS232 a message, then calling the timer function and passing it the name of the function which it should call, should the message response fail, seems like an economical way to code. I just don't know what to use for the ???? in the MyTimer function or if it can be done.

I hope all of that makes sense, and I hope there is some way to do this.

Thanks in advance for your help.

John Hagen

jteagle@home
June 19th, 1999, 02:56 PM
I can see no problem, so here goes: let's name the function you want called from within another FunctionToCall(), and the function which will call it FunctionThatCallsIt(). If the prototype for FunctionToCall() is of the form

return_type FunctionToCall(type1 var1, type2 var2)

then FunctionThatCallsIt() would be defined as:

BOOL FunctionThatCallsIt(return_type (*lpfnFNToCall)(type1, type2) )

(where lpfnFNToCall is any name you choose).

Then, to call the passed function, use this:

(*lpfnFNToCall)(var1, var2);

I have verified that this works when both functions are global (not members of classes). I'll leave it to others to figure out how to make it work under other circumstances ('coz I don't know!).

John Hagen
June 19th, 1999, 05:01 PM
Thanks for the input. I'll give it a try!

John