Not Yet Answered # Can a function gives back function without pointers?

JasonHippy 724 Discussion Starter merse enlightened firstPerson 761 Discussion Starter merse firstPerson 761

0

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?

0

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?

0

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!

0

>> 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

0

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?

0

>>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.

Recommended Articles

Help! I want to create a java program that finds the highest even integer among the values entered by the user. Stop asking values when a value less than 1 have been entered. If no even integer is entered, display "No Even Integer"

Here is the sample output that I ...

Hello All ...

Iam Getting An Error With try to excecute the stored procedure .

I have Have Sql database , the stored procedure like so :

```
USE [MPRS]
GO
/****** Object: StoredProcedure [dbo].[Search_Licenses_By_Number] Script Date: 26-Nov-16 8:06:52 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE ...
```

I don’t want at this stage work on a big separate project as I've already got plenty ...