# Tough problem with function pointers

• 12-21-2009
Montejo
Tough problem with function pointers
Hi everyone,
I'm working on a program for numericaly solving differential equations, I have an index i which runs from -LATICE to LATICE. This index undergoes a coordinate substitution according to the problem and is mapped to [0, infinity) in radial coordinates, (-infinity,infinity) for one dimensional problems or [-N,N] for limited one-dimensional problems. I use a function pointer to choose which of the mapping function will be used during run-time.
The problem comes with the third mapping, the scaling [-N,N] is just i*N but N is also determined during run-time. So, is there a way of defining a function which is given as parameter N and returns a pointer to a function which multiplies i with N?

I tried a bit or round-working, I made a function with variable number of arguments, if more than one parameter is given, the second one sets a static variable N. The first parameter is just i and it returns i*N. And to make it compatible with the function pointer (which needs fixed number of arguments) it is called once to set N and then I use a function with just calls this one without second parameter. So, kind of ugly solution.

Anyone knows a solution to the actual problem?
• 12-21-2009
itCbitC
Yep! you can have an array of pointers to functions, each returning a pointer to a function. For the the exact solution it would help to show some code.
• 12-21-2009
Montejo
Quote:

Originally Posted by itCbitC
Yep! you can have an array of pointers to functions, each returning a pointer to a function. For the the exact solution it would help to show some code.

The problem is each function in the array should be multiplication with N (varying with the index) but
a) N is not integer but double
b) even if integer that would mean writing infinite multiplication functions for each N

Thanks anyway
• 12-21-2009
bithub
Quote:

Originally Posted by Montejo
The problem is each function in the array should be multiplication with N (varying with the index) but
a) N is not integer but double

How is that a problem?
Quote:

Originally Posted by Montejo
b) even if integer that would mean writing infinite multiplication functions for each N

Nothing in your problem description indicates this to be the case. It might help for you to post some code.
• 12-21-2009
itCbitC
Quote:

Originally Posted by Montejo
The problem is each function in the array should be multiplication with N (varying with the index) but
a) N is not integer but double
b) even if integer that would mean writing infinite multiplication functions for each N

Thanks anyway

Totally confused with what you'ave said above. Post some code and someone might be able to help you out.
• 12-21-2009
King Mir
Quote:

Originally Posted by Montejo
So, is there a way of defining a function which is given as parameter N and returns a pointer to a function which multiplies i with N?

This is easier in C++.

I reccomend avoiding functions with variable number of arguments.

If you only need one possible value N at a time you could use a global or static variable. If you have a finite list of possible values of N, then you can have multiple functions for each value of N. Macros can make defining them not take so much code.

Otherwise there is no way to avoid being ugly. You basically need the function that is returned to have an extra data parameter to store N. Since you seems to be storing it in a pointer that can also point to functions that don't have data, those functions will still need the extra unused parameter. For symmetry you can make it so that the functions that generate the other types of mapping also return extra useless data.

Effectively you'd be implementing OOP in C. So if you can, use C++, which implements OOP for you.

For those that don't understand the question, the OP wants to do this:
Code:

```//C++0x code int (*func(N))(int){   return [=](int i)->{return i*N;}; }```
@Montejo
Real C++ isn't quite this easy, but it is easier than C.
• 12-21-2009
laserlight
Quote:

Originally Posted by King Mir
Effectively you'd be implementing OOP in C.

No, functional programming, not OOP. I actually tried to research how to use functional programming in C when I read that, but the answers to be found online appeared too complicated to bother if Montejo's problem can be solved differently, hence I refrained from a reply.
• 12-21-2009
King Mir
EDIT: I guess that's true. But it can be solved with OOP.
• 12-21-2009
laserlight
Quote:

Originally Posted by King Mir
It can be done with a lambda, or it can be done with functors or it can be done with objects and methods. Lambda's are functional constructs, objects are OOP. Functors are in between. So either functional programming or OOP will do.

The "objects and methods" option that you talk about is just a way to simulate a functor by designating a named method as a callback, even though this does not allow the object to be "called" like a function. I do not agree with the identification of the use of objects as evidence of OOP per se. Sure, if there is an inheritance hierarchy of function objects, then it is obvious that OOP is involved, but here I feel that the intent to return a function from a function and apply the function returned is more on the lines of functional programming. So, the use of a function object here is effectively simple functional programming.
• 12-22-2009
King Mir
Yeah, I changed my mind after I wrote that.
• 12-22-2009
itCbitC
Quote:

Originally Posted by Montejo
So, is there a way of defining a function which is given as parameter N and returns a pointer to a function which multiplies i with N?

That's the only part I get because the next paragraph obfuscates it all; and the above can be implemented as
Code:

`int (*(f(int N))) (int);  /* prototype of function returning pointer to function that takes and returns an int */`