In C++ we can define functions and their arguments can be not just variables, but functions also, for example:

double Integral(double func(const double),const double a, const double b){...};

(without using any pointer)

But can a function return with a function?
like a differential operator?

As you've pointed out, you can pass a pointer to a function as a parameter to a function.

And yes, it is possible to make a function return a pointer to a function, but the syntax is a little messy!

But no, you can't do it without pointers as far as I'm aware.

I haven't had anywhere near enough coffee this morning to be able to put a coherent example together, but take a look at the tutorial here:
http://www.newty.de/fpt/fpt.html

The tutorial explains all you need to know about function pointers and contains links to downloadable .pdf versions of the tutorial (in french and english) plus zipped source code!

Cheers for now,
Jas.

p.s. Question to admins/moderators: Should I download and zip up the files on the referenced page, and then post them here as an attachment (in case the referenced page gets taken down in the future)? Or should I leave it as is?

Thanx the tutorial, I've read it, but I still have question.

I would like return with a function or function pointer.
It is possible if the return value is an existing function.
The example in the tutorial is the following:

typedef float(*pt2Func)(float,float);

pt2Func GetPtr2(const char opCode)
{
  if(opCode == ’+’) return &Plus;
  else return &Minus;
}

But Plus and Minus are predefined functions, like sin or cos,
but how can i retun with sin+cos or sin+1?
Can i write a function which get lots of parameters or function pointers,
and return a combined function like 1+sin+cos?

Hi Merse

As i understand it you would like to construct an answer function to which you applied logic for integration. Then you want to return that constructed function. Thats where the trouble comes in. You can only return a reference to a single function so it wont be that simple. You could ofcourse predefine your own function for sin + cos for example... but obviously we don't want to predefine every possible integral in existence...

Luckily if you think about it a bit its not impossible. You could (meaning this is neither optimal nor the right way to do it) use a vector, array, stack or whatever tickles your fancy to save a reference to each function that would form part of your answer (don't forget constants - write this one yourself) then iterate through each element in sequence for whatever you need this for

If for instance you need to apply a value to this answer you would do something like this: (pseudocode to follow)

int applyValue(int value)
{

int numericalAnswer = 0;
for each element in answer
numericalAnswer += functionInThisElement (value)

return numericalAnswer ;
}

All you need to know to make this work is in that tutorial above. I hope I understood your problem correctly!

>> But Plus and Minus are predefined functions, like sin or cos,
but how can i retun with sin+cos or sin+1?

You will have to create a composite functions.

typedef float(*p_f)(float);

//way 1
float composite(float arg){
return sin( cos( arg) );
}
float composite2( float arg, p_f f1 = cos, p_f f2 = sin ) {
   return (*f1)( (*f2)(arg) );
}

p_f retriveSomeFunc(){
  return  &composite;
  //or return &composite2;

composite 1 is straight cos and sin, composite 2 has default cos and sin but you can pass it functions later on if you
decide to change.

maybe something like this :

p_f  specialMath = retriceSomeFunc(); //say it returned composite 2

//if specialMath is pointer to the composite 2 function then we can do this :
specialMath(1.0); // cos( sin ( 1) );
specialMath(1.0, &sin , &cos); // sin( cos( 1) );

 //dangerous because sqrt is not defined for negative numbers, because log can produce negative numbers
specialMath(1.0, sqrt, log ); // sqrt( log( 1.0) )

Realize that this all is not tested, and I don't have much experience with pointer functions as I should have, so I might be misleading somewhere.

Edited 7 Years Ago by firstPerson: n/a

But specialMath(1.0, &sin , &cos); is not a single variable function,
I cannot put in the Integration:

typedef double real_func(double); 
double Integral(real_func func, double a, double b){...}

Lets simplify my question:
It is possible to create a function like this:

real_func MyOp(real func f){...}
which gives back f+1 as a function?

Sorry I mean:

typedef real_func *real_fp;
real_fp MyOp(real func f){...}

>>But specialMath(1.0, &sin , &cos); is not a single variable function,
I cannot put in the Integration:

Why not make a wrapper for it then, or use bind*

This article has been dead for over six months. Start a new discussion instead.