Passing functions as arguments

This is a discussion on Passing functions as arguments within the C++ Programming forums, part of the General Programming Boards category; I remember reading a while back on how to pass a function into a function, but I forgot where. Does ...

  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
    Budding Synth Programmer samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Trefforest
    Posts
    368
    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.
    MSVC++ 6.0

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, 05:12 PM
  2. Problem Passing Values between functions
    By jamez05 in forum C++ Programming
    Replies: 2
    Last Post: 05-02-2007, 02:21 PM
  3. Passing functions between files
    By Duskan in forum C Programming
    Replies: 9
    Last Post: 04-17-2007, 08: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21