Passing a function as an argument

This is a discussion on Passing a function as an argument within the C Programming forums, part of the General Programming Boards category; Sorry if this has been asked before but I couldn't find it anywhere. My question is how do you pass ...

  1. #1
    Registered User Xzyx987X's Avatar
    Join Date
    Sep 2003
    Posts
    107

    Passing a function as an argument

    Sorry if this has been asked before but I couldn't find it anywhere. My question is how do you pass a function as an argument into another function. I don't mean pass the return value, I mean passing the function itself so the recieving funtion can use it as much as neccessary in order to get the values it needs.

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    Use a function pointer:
    Code:
    #include <stdio.h>
    
    int square(int x) {
       return x * x;
    }
    
    int cube(int x) {
       return x * x * x;
    }
    
    void eval(int x, int (*func)(int)) {
       printf("%d\n", (*func)(x));
    }
    
    int main() {
       eval(3, &square);
       eval(3, &cube);
       return 0;
    }

  3. #3
    Registered User Xzyx987X's Avatar
    Join Date
    Sep 2003
    Posts
    107
    I put this function prototype, but it's generating quite a few compile errors. Mind telling me where I went wrong?

    void DrawCollumn(char * (*RowText)(UINT)(DASMVIEWERINFO *), (DASMVIEWERINFO *) dvp);

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >char * (*RowText)(UINT)(DASMVIEWERINFO *)
    RowText is a pointer to function that takes a single UINT as an argument and returns a pointer to char. What were you expecting (DASMVIEWERINFO *) to do?

    >(DASMVIEWERINFO *) dvp
    Is this a cast, or what?
    My best code is written with the delete key.

  5. #5
    Registered User Xzyx987X's Avatar
    Join Date
    Sep 2003
    Posts
    107
    Oops, missed that one. Ok, with this we're down to 3 compile errors:

    void DrawCollumn(char * (*RowText)(int)(DASMVIEWERINFO *), DASMVIEWERINFO * dvp)

    Edit: Err.. sorry, I didn't read the first part of your post for some reason. I thought that was how you were supposed to do it if the function took multiple arguments. I tried it this way though, and it worked:

    void DrawCollumn(char * (*RowText)(UINT, DASMVIEWERINFO *), DASMVIEWERINFO * dvp)
    Last edited by Xzyx987X; 04-20-2004 at 08:06 PM.

  6. #6
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    Code:
    char * (*RowText)(int, DASMVIEWERINFO *)
    Its just like a normal function prototype except with a pointer star and parenthesis around the name.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Oops, missed that one. Ok, with this we're down to 3 compile errors:
    Here's a novel idea... How about posting your errors so we don't have to play guessing games? Hey, here's an even better idea, how about posting the function prototype of the function you expect to pass.

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User Xzyx987X's Avatar
    Join Date
    Sep 2003
    Posts
    107
    Quote Originally Posted by quzah
    Here's a novel idea... How about posting your errors so we don't have to play guessing games?
    Well, I figured the anwser would be obious enough to someone who'd done this kind of thing before I wouldn't have to.
    Quote Originally Posted by quzah
    Hey, here's an even better idea, how about posting the function prototype of the function you expect to pass.
    Beacuse I didn't write one yet .

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I figured the anwser would be obious enough to someone who'd done this kind of thing before
    It's obvious what you're doing wrong, but determining what you want to do from what you're doing wrong is more difficult. Is this what you were trying to do?
    Code:
    void DrawCollumn(char * (*RowText)(int, DASMVIEWERINFO *), DASMVIEWERINFO * dvp);
    My best code is written with the delete key.

  10. #10
    Registered User Xzyx987X's Avatar
    Join Date
    Sep 2003
    Posts
    107
    Ah, good point. And yes that's what I meant to do. I edited my third post when I figured it out, but by that time two other people had already posted...

  11. #11
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,643
    Typedefs are handy here, too.

    typedef char * (*RowTextFunction)(int, DASMVIEWERINFO *);

    void DrawCollumn(RowTextFunction RowText, DASMVIEWERINFO * dvp);
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. Passing A Function Into A Constructor
    By fourplay in forum C++ Programming
    Replies: 6
    Last Post: 03-15-2009, 06:06 AM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 01:28 PM
  5. Passing my array to function
    By pooty tang in forum C Programming
    Replies: 8
    Last Post: 09-15-2004, 12:19 PM

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