Thread: function as a input parameter

  1. #1
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669

    Unhappy function as a input parameter

    Hi!

    Can someone please help me with this code? I'm lost. I get one warning and two error messages.

    ".../problem.c(19): warning C4047: 'function' : 'int (__cdecl *)(int,int)' differs in levels of indirection from 'int'"

    ".../problem.c(29): error C2065: 'a' : undeclared identifier"

    ".../problem.c(29): error C2065: 'b' : undeclared identifier"

    Code:
    # include <stdio.h>
    # include <conio.h>
    
    
    int  Multiply (int a, int b);
    void WriteOut (int (*Multiply) (int a, int b));
    void WaitForKey (void);
    
    
    int main ()
    {
    	int a = 0, b = 0;
    
    
    	printf ("Enter first number: ");
    	scanf ("%d", &a);
    	printf ("Enter second number: ");
    	scanf ("%d", &b);
    	WriteOut ( Multiply (a, b) );
    	WaitForKey ();
    	return 0;  
    }
    int Multiply (int a, int b)
    {
        return (a * b);
    }
    void WriteOut (int (*Multiply) (int a, int b))
    {
        printf ("Result = %d", Multiply (a, b));
    }
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    Your function WriteOut expects a pointer to function only, you cannot pass a and b the way you currently do. If you want the variables also be passed, pass them seperately.

    Prototype WriteOut as:
    void WriteOut (int (*Multiply) (int, int), int a, int b);

    And use it as:
    WriteOut (Multiply, a, b);

  3. #3
    Registered User MisterBadger's Avatar
    Join Date
    Mar 2002
    Posts
    16
    Hi GaPe

    I used your Multiply function with printf to display a*b.

    Do you really need the Multiply function? This line would give the same result:

    printf("\nProduct of %d and %d is %d\n\n",a,b, a*b);

    Here's my code:

    Code:
    # include <stdio.h>
    # include <conio.h>
    
    int  Multiply (int a, int b);
    //void WriteOut (int (*Multiply) (int a, int b));
    //void WaitForKey (void);
    
    int main ()
    {
    	int a = 0, b = 0;
    
    
    	printf ("Enter first number: ");
    	scanf ("%d", &a);
    	printf ("Enter second number: ");
    	scanf ("%d", &b);
    //	WriteOut ( Multiply (a, b) ); // I used this code to display the result:
    
    printf("\nProduct of %d and %d is %d\n\n",a,b, Multiply(a,b));
    
    //	WaitForKey (); // This function was not defined.
    		       // Does the following code do what you intended...
                           // Program will wait for next keystroke...
       printf("Any key to clear screen and end prog > ");
    	getch();
    	clrscr();
    	printf("END OF PROG!\n\n");
    	return 0;
    }
    int Multiply (int a, int b)
    {
    	 return (a * b);
    }
    
    /*void WriteOut (int (*Multiply) (int a, int b))
    {
    	 printf ("Result = %d", Multiply (a, b));
    } I didn't use this function...*/
    Cheers from Badger :-)

  4. #4
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    Gape probably wants to do some generic programming. With use of a function pointer, he can also pass other functions of the same type to WriteOut, like

    int sum (int a, int b);
    int subtract (int a, int b);
    int divide (int a, int b);
    etc.

  5. #5
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    MisterBadger you're totally off of what I want.

    Shiro is right. I just want to know how to use function as a input parameter in other function. So please help me out with this.

    One more thing. What are the advantages/disadvantages for that kind of use of functions?
    Last edited by GaPe; 02-02-2003 at 12:12 PM.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>I just want to know how to use function as a input parameter in other function. So please help me out with this.
    Shiro already gave you the answer. Here's a working version of your code:
    Code:
    # include <stdio.h>
    # include <conio.h>
    
    
    int  Multiply (int a, int b);
    void WriteOut (int (*Multiply) (int, int), int, int);
    void WaitForKey (void);
    
    
    int main ()
    {
    	int a = 0, b = 0;
    
    
    	printf ("Enter first number: ");
    	scanf ("%d", &a);
    	printf ("Enter second number: ");
    	scanf ("%d", &b);
    	WriteOut ( Multiply, a, b );
    	//WaitForKey ();
    	return 0;  
    }
    int Multiply (int a, int b)
    {
        return (a * b);
    }
    void WriteOut (int (*Multiply) (int, int), int a, int b)
    {
        printf ("Result = %d", Multiply (a, b));
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 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
  2. Consequence of const-ifying a function parameter
    By hzmonte in forum C Programming
    Replies: 3
    Last Post: 08-24-2006, 01:28 PM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. Custom Made Safe Input Function
    By Beast() in forum C Programming
    Replies: 6
    Last Post: 08-21-2004, 10:19 PM