Thread: I wonder ...... (functions)

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    28

    I wonder ...... (functions)

    Is it possible to write a function deffinition which includeds a function as an argument ?

    for ex.

    typedef struct item item;
    struct item{
    ......... }


    item *add(item *p1, item* newnode()){.........}

    item *newnode() {.....}
    -----------------------------------------------------------------
    -----------------------------------------------------------------
    or maybe like this:

    typedef item*newnode(){.....} node;

    item *add(item*p1, node){.....}

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Of course! Here, this should work:


    Code:
    
    
    int All( char str[], int Func() )
    {
     int i;  
    
    for( i = 0; i < strlen(str); i++ )
       {
         if( !Func( str[i] ) )
         return false;
       }
    
     return true;
    }
    
    
    
    
    
    
    
    
    void print(char msg[])
    {
     clrscr();
     printf("\n\n\n\n   %s \n", msg);
     getch();
    }
    
    
    
    
    
    
    
    
    
    int main()
    {
    
     char string[] = "1234567";
     
     if( All( string, isdigit ) )
       print("The string is all numbers.");
     else
       print("The string is NOT all numbers.");
     
     if( All( string, isalpha ))
       print("The string is all characters.");
     else
       print("The string is NOT all characters.");
    
    
    return 0;
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    And another example:
    Code:
    int add(int a, int b) { return a+b; }
    int multiply(int a, int b) { return a*b; }
    int calculate(int (*f)(int,int), int a, int b) { return f(a, b); }
    
    int main()
    {
    	int a = 4, b = 5;
    
    	printf("%d\n", calculate(add, a, b) );
    	printf("%d\n", calculate(multiply, a, b) );
    	return 0;
    }

  4. #4
    *
    Guest
    Another (my preferred) method:

    Function Pointers.

    ----


    Code:
    typedef int (*MULTIPLYFUNC)(int,int);
    
    typedef struct
       {
       int                     val_1;
       int                     val_2;
       MULTIPLYFUNC  theFuncPtr;
       }myStruct;
    
    int main(void);
    int MyMultiplyFunc(int,int);
    
    int main(void)
       {
       myStruct               infoStruct;
       MULTIPLYFUNC     fPtr;
       int                        result;
    
       fPtr = NULL;
    
       infoStruct.val_1 = 5;
       intoStruct.val_2 = 10;
       infoStruct.theFuncPtr = (MULTIPLYFUNC*)&MyMultiplyFunc;
    
       fPtr = infoStruct.theFuncPtr;
       result = fPtr(infoStruct.val_1,infoStruct.val2);
    
       return(0);
       }
    
    int MyMultilyFunc(int arg1,int arg2)
       {
       return(arg1*arg2);
       }

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    28
    Thanks for the help.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    infoStruct.theFuncPtr = (MULTIPLYFUNC*)&MyMultiplyFunc;

    fPtr = infoStruct.theFuncPtr;
    result = fPtr(infoStruct.val_1,infoStruct.val2);
    Actually, there is no real reason to even have 'fPtr' here. I suppose it was really for just an example, but why bother having a function pointer as a stucture member if you don't do anything with it?

    Code:
    myStruct m;
    
    m.theFuncPtr = &someFunc;
    result = m.theFuncPtr( m.val_1, m.val_2 );
    In either case, nothing in your example actually addressed the original question, which was "Can I have a function as a parameter in another function?"

    The answer, is yes. The simplest example of this is qsort.

    Quzah.
    Last edited by quzah; 06-18-2002 at 03:33 AM.
    Hope is the first step on the road to disappointment.

  7. #7
    *
    Guest
    Yes, quzah, it was to illustrate using functions pointers, which can be passed in other functions:

    Code:
    typedef int (*MULTIPLYFUNC)(int,int);
    
    int MultiplyIntsFunc(int,int);
    int AMathFunc(MULTIPLYFUNC,int,int);       /*  <-- note */
    int main(void);
    
    
    int main(void)
       {
       int  result;
    
       result = AMathFunc(&MultiplyIntsFunc,5,5);
       return(0);
       }
    
    int AMathFunc(MULTIPLYFUNC fPtr,int a,int b)
       {
       int   result;
    
       result = fPtr(a,b);
       return(result);
       }
    
    int MultiplyIntsFunc(int a, int b)
       {
       return(a*b);
       }

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > result = AMathFunc(&MultiplyIntsFunc,5,5);
    Just to point out that the & is entirely optional in this context

    You get a function pointer from just the name of the function
    result = AMathFunc(MultiplyIntsFunc,5,5);

  9. #9
    *
    Guest
    Yes, that is entirely correct. I merely used it to demonstrate that an address was being supplied.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 05:30 PM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM