how to pass a function as a parmeter to a function?

This is a discussion on how to pass a function as a parmeter to a function? within the C Programming forums, part of the General Programming Boards category; OS: linux compiler: gcc version 3.3.5 (Debian 1:3.3.5-5) editor: vi level: beginner Hello, I am working through a tutorial at: ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    2

    how to pass a function as a parmeter to a function?

    OS: linux
    compiler: gcc version 3.3.5 (Debian 1:3.3.5-5)
    editor: vi
    level: beginner


    Hello,

    I am working through a tutorial at: http://vergil.chemistry.gatech.edu/r...al/basic2.html, which demonstrates how to pass a function as parameter to a function.

    I've tried replicating the method shown, but come up against some problems:

    Code:
    #include <stdio.h>
    
    int squared(int x){return x*x;};
    int cubed(int y ){return y*y*y;};
    int result(int F(int))
    {
    
            int a;
            a=F(int);
            return a;
    };
    
    
    int main()
    {
            int x = 1;
    
            for (;x < 10;x++)
            {
                    printf("squared%i\t",result(squared(x)));
                    printf("cubed%i\t",result(cubed(x)));
            }
            return 0;
    }
    errors returned compiled with gcc play.c :
    play.c: In function `result':
    play.c:9: error: parse error before "int"
    play.c: In function `main':
    play.c:20: warning: passing arg 1 of `result' makes pointer from integer without a cast
    play.c:21: warning: passing arg 1 of `result' makes pointer from integer without a cast


    I would be grateful if someone could point me in the right direction on how to pass function(s) as parameter(s)

    Thanks

    Billy

    P.S Already googled

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,834
    How about
    Code:
    #include <stdio.h>
    
    int squared(int x){return x*x;};
    int cubed(int y ){return y*y*y;};
    int result(int (*F)(int), int value)
    {
            int a;
            a=F(value);
            return a;
    };
    
    
    int main()
    {
            int x;
    
            for (x=1;x < 10;x++)
            {
                    printf("squared %i\t",result(squared,x) );
                    printf("cubed %i\n",result(cubed,x) );
            }
            return 0;
    }
    Check out http://www.function-pointer.org/
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    2

    Smile Thanks Salem

    Thanks Salem for your time and help!

    Regards

    Billy

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 01:13 AM
  2. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 09:56 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 06:06 PM

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