Click to See Complete Forum and Search --> : Function pointers...I don't know what they are
spetsacdc
March 3rd, 2008, 09:26 PM
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
Lindley
March 3rd, 2008, 09:40 PM
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).
7stud
March 3rd, 2008, 09:45 PM
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.
7stud
March 3rd, 2008, 09:51 PM
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:
#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;
}
Lindley
March 3rd, 2008, 10:13 PM
My own knowledge of function pointers is limited, but I'm fairly certain that in some cases you'd have to write:
(*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.
7stud
March 3rd, 2008, 10:51 PM
My own knowledge of function pointers is limited, but I'm fairly certain that in some cases you'd have to write:
(*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/function-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
potatoCode
March 3rd, 2008, 11:49 PM
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
#include "g.h"
#include "f.h"
// pass by pointer
int g(int* y)
{
return f(*y);
}and f.cpp// 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!
laserlight
March 4th, 2008, 12:19 AM
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.
spetsacdc
March 5th, 2008, 12:07 AM
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.
mgrey
March 5th, 2008, 12:20 PM
If you are not allowed to use if keyword, maybe something like this ...
#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;
}
:)
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.