CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Mar 2008
    Posts
    17

    Function pointers...I don't know what they are

    In my program, someone enters a value for x, and this value goes into a function and the answer is returned. Now, I have a main.cpp, functions.h, and functions.cpp

    There are 2 functions f(x) and g(x).

    In main.cpp:

    The user enters a value for x
    The user is prompted to enter 1 to use f(x) or 2 for g(x).
    functions(x) is called and finds f(x) or g(x)

    Now, in my assignment it says:

    "All functions and the main program must be defined in separate c++ source files and all functions must have separate header files). There is a way in C++ to pass a pointer to a function as an argument in a function call. Use this method to avoid writing two versions of the source code and run the code once to obtain the results."

    I have read a lot of tutorials on function pointers and I still do not know how to implement this. From what I read, I would need two functions, one for f(x) and one for g(x). But, according to the assignment I would have to put them in seperate .cpp and .h files. But, this would be writing two versions of the source code.

    I'm not sure how I am supposed to implement a function pointer without having seperate functions (one for f(x) and one for g(x)). I could easily just use if statements, but its not allowed.


    Any ideas?

    Thanks

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Function pointers...I don't know what they are

    The problem with function pointers is that in toy cases they seem a bit pointless. You only realize how useful they are once you get into more complex stuff.

    In generic terms:
    1) Define a function pointer func that matches the signatures of functions f and g.
    2) Read input. Depending on input, assign either f or g to func.
    3) Call whichever function func points to by dereferencing func.

    Let's say you have 10 places where f or g need to be called. The "normal" way, you'd need 10 if statements. With a function pointer, you only need 1 (to set the pointer).

  3. #3
    Join Date
    Oct 2004
    Posts
    296

    Re: Function pointers...I don't know what they are

    From what I read, I would need two functions, one for f(x) and one for g(x). But, according to the assignment I would have to put them in seperate .cpp and .h files. But, this would be writing two versions of the source code.
    The bottom line is that every function you call has to be defined. You cannot call a function that is not defined. Your assignment starts off by saying you need two functions: one called f and one called g. That means you have to have two function definitions: one defining f and one defining g.

    I have no idea what the admonition "do not duplicate the source code" means.

  4. #4
    Join Date
    Oct 2004
    Posts
    296

    Re: Function pointers...I don't know what they are

    Quote Originally Posted by Lindley
    The problem with function pointers is that in toy cases they seem a bit pointless. You only realize how useful they are once you get into more complex stuff.

    In generic terms:
    1) Define a function pointer func that matches the signatures of functions f and g.
    2) Read input. Depending on input, assign either f or g to func.
    3) Call whichever function func points to by dereferencing func.

    Let's say you have 10 places where f or g need to be called. The "normal" way, you'd need 10 if statements. With a function pointer, you only need 1 (to set the pointer).
    Uhhmm...how about this:
    A) A function name is a pointer to the function.

    In generic terms:
    1) Define a function pointer pfunc that matches the signatures of functions f and g.
    2) Read input. Depending on input, assign either f or g to pfunc.
    3) Call the function pfunc by supplying the correct arguments.
    Here is an example:
    Code:
    #include <iostream> 
    using namespace std;
    
    void some_func(int n)
    {
    	cout<<n<<endl;
    }
    
    
    int main ()  
    {
    	void(*pfunc)(int);
    	pfunc = some_func;
    	
    	pfunc(10);
    		 
    	cout<<">>>Hit Enter to Quit";
    	cin.get();
    	return 0;
    }
    Last edited by 7stud; March 3rd, 2008 at 10:56 PM.

  5. #5
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Function pointers...I don't know what they are

    My own knowledge of function pointers is limited, but I'm fairly certain that in some cases you'd have to write:
    Code:
    (*pfunc)(10);
    I'm not sure if that's a compiler thing, or a C-vs-C++ thing, or what. But I definitely recall seeing that as the preferred way.

    The above is certainly cleaner, though. Almost what you'd expect from a language that supports functions as first-class objects, like one of the ML variants.

  6. #6
    Join Date
    Oct 2004
    Posts
    296

    Re: Function pointers...I don't know what they are

    Quote Originally Posted by Lindley
    My own knowledge of function pointers is limited, but I'm fairly certain that in some cases you'd have to write:
    Code:
    (*pfunc)(10);
    I'm not sure if that's a compiler thing, or a C-vs-C++ thing, or what.
    Me neither. This tutorial says both the &(to get the address of a function) and *(to dereference a function pointer) are optional:

    http://www.cprogramming.com/tutorial...-pointers.html

    And the Function Pointer Tutorial recommends using & because some compilers require it, and the tutorial implies dereferencing is optional:

    http://www.newty.de/fpt/fpt.html#callconv
    Last edited by 7stud; March 4th, 2008 at 12:05 AM.

  7. #7
    Join Date
    Jan 2008
    Location
    California, USA
    Posts
    822

    Re: Function pointers...I don't know what they are

    Hello spetsacdc,

    I'm a beginner here, but like 7stud said, can't it be that you're leaving something out of the context?

    because, you said if statement is now allowed (and I assume neither is switch), but you said a user is prompted to enter a choice, how is this possible without a condition?

    Here's my best guess on what must be done.
    1. both functions must give(not necessairly return) same End Result.
    2. we need to create 5 seperate files. one for main.cpp, 2 for g.h/g.cpp, and 2 for f.h/f.cpp.
    3. Let's assume "avoid two versions of the source code" as "One definition in one function is not seen in the other.
    4. One of either function must accept a pointer as an ARGUMENT.
    5. No conditional statements are allowed.

    given all these, I believe (hopefully) either of the functions must call the other.
    for instance; g.cpp would be something like
    Code:
    #include "g.h"
    #include "f.h"
    
    // pass by pointer
    int g(int* y)
    {
         return f(*y);
         }
    and f.cpp
    Code:
    // pass by value
    int f(int y)
    {
         int twice(0);
         twice = y * 2;
            
         return twice;
         }
    as you can see, we did not write two versions of the same source code(#3)
    and both functions give the same result(#1) and satisfies #4 and the rest with the remainding header files.

    but somehow,
    I have a feeling (as usual...) what 7stud and Lindley said is more likely to be your answer.

    see ya!

  8. #8
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Function pointers...I don't know what they are

    My own knowledge of function pointers is limited, but I'm fairly certain that in some cases you'd have to write:

    I'm not sure if that's a compiler thing, or a C-vs-C++ thing, or what.
    For both C and C++, both (*pfunc)(10) and pfunc(10) are legal.

    But I definitely recall seeing that as the preferred way.

    The above is certainly cleaner, though. Almost what you'd expect from a language that supports functions as first-class objects, like one of the ML variants.
    I prefer the pfunc(10) version: if you substitute the function pointer with a function object, the code need not change.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  9. #9
    Join Date
    Mar 2008
    Posts
    17

    Re: Function pointers...I don't know what they are

    Quote Originally Posted by potatoCode
    Hello spetsacdc,

    I'm a beginner here, but like 7stud said, can't it be that you're leaving something out of the context?

    because, you said if statement is now allowed (and I assume neither is switch), but you said a user is prompted to enter a choice, how is this possible without a condition?
    Maybe, I was trying to summarize what the assignment was. The only thing mentioned about function pointers in the assignment is the following:

    "There is a way in C++ to pass a pointer to a function as an argument in a function call. Use this method to avoid writing two versions of the source code and run the code once to obtain the results."

    I guess I was wrong in assuming the user will enter 1 or 2 to choose which function to use.

  10. #10
    Join Date
    Jul 2007
    Posts
    31

    Re: Function pointers...I don't know what they are

    If you are not allowed to use if keyword, maybe something like this ...

    Code:
    #include <stdio.h>
    
    void fn_zero()
    {
    	printf("zero\n");
    }
    
    void fn_one()
    {
    	printf("one\n");
    }
    
    
    void function(void (*p_fn) ())
    {
    	p_fn();
    }
    
    
    int main()
    {
    	void (*p_fn[]) () = {fn_zero, fn_one};
    
    	function(p_fn[0]);
    	function(p_fn[1]);
    
    	return 0;
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured