+ Reply to Thread
Results 1 to 2 of 2

Thread: How can I pass function as a parameter/argument of another function?

  1. #1

    How can I pass function as a parameter/argument of another function?

    I wanted to get a callback mechanism implemented.

    I have heard that this is passing a function to another function.

    I want to know how to pass function as an argument in another method.

    It should be like:

    Code:
    void RegisterFunction(anotherfunction) ;
    where another function can be:

    Code:
    void anotherfunction(void *) ;

  2. #2
    Below example can be used as sample.

    It registers a callback and makes a call to the registered callback.

    Code:
    void FunctionToBeCalled(void *pUserData)
    {
    	int *pData = (int *) pUserData ;
    }
    
    typedef void (*PFNFUNCTIONTOBECALLED) (void *) ;
    
    void RegisterCallback(PFNFUNCTIONTOBECALLED pfnFunctionToBeCalled)
    {
    	int nData = 42 ;
    	pfnFunctionToBeCalled((void *) &nData) ;
    }
    
    int main()
    {
          RegisterCallback(&FunctionToBeCalled) ;
          return 0 ;
    }

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts