Thread: Passing functions as arguments

  1. #1
    Unregistered
    Guest

    Passing functions as arguments

    I remember reading a while back on how to pass a function into a function, but I forgot where. Does anyone know how to do this? Can you provide a simple example please? Or does anyone know where I can find info on this. I have been searching for a while and havent found anything.

    Thanks

    Eric

  2. #2
    Registered User samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Newport
    Posts
    382
    Code:
    #include <iostream.h>
    
    void one (void);
    void two (void);
    
    void doThis (void (*f) (void));
    
    void main (void)
    {
    	void (*func) (void) = one;
    
    	doThis (func);   // Pass an already declared pointer, or...
    	doThis (two);   // Pass the address of function directly.
    }
    
    
    void one (void)
    {
    	cout << "One.\n";
    }
    
    void two (void)
    {
    	cout << "Two.\n";
    }
    
    void doThis (void (*f) (void))
    {
    	f ();
    }
    Remeber, the functions must be of the same return and arg types.
    Last edited by samGwilliam; 02-13-2002 at 11:17 PM.
    Current Setup: Win 10 with Code::Blocks 17.12 (GNU GCC)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Functions Taking Dif. Numbers of Arguments at Dif. Times?
    By bengreenwood in forum C++ Programming
    Replies: 6
    Last Post: 03-23-2009, 04:12 PM
  2. Problem Passing Values between functions
    By jamez05 in forum C++ Programming
    Replies: 2
    Last Post: 05-02-2007, 01:21 PM
  3. Passing functions between files
    By Duskan in forum C Programming
    Replies: 9
    Last Post: 04-17-2007, 07:44 AM
  4. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  5. Replies: 1
    Last Post: 01-20-2002, 11:50 AM