anyway to give extra parameter to an existing function?

I am using some recipes to do some calculation. But there are
situation that I need to give extra parameters to the existing
function. For example, the recipe has a function called:
foo (int x, int y)
But now I want to give another parameter, say, float z.
One way is I just change the recipe function to accept 3 parameters,
but I think there should be a more elegant way to do that. Because I
don't want to mess up the existing library/functions.

Advertisements

Dave wrote:
> I am using some recipes to do some calculation. But there are
> situation that I need to give extra parameters to the existing
> function. For example, the recipe has a function called:
> foo (int x, int y)
> But now I want to give another parameter, say, float z.
> One way is I just change the recipe function to accept 3 parameters,
> but I think there should be a more elegant way to do that. Because I
> don't want to mess up the existing library/functions.

Dave wrote:
> I am using some recipes to do some calculation. But there are
> situation that I need to give extra parameters to the existing
> function. For example, the recipe has a function called:
> foo (int x, int y)
> But now I want to give another parameter, say, float z.
> One way is I just change the recipe function to accept 3 parameters,
> but I think there should be a more elegant way to do that. Because I
> don't want to mess up the existing library/functions.
>

You can see that DP funk(Vec_I_DP &) is a function call. Inside
NR::amoeba, funk is called several times to do the calculation.
The user are supposed to provide a function of this type. For example,
the following;

if I use NR::amoeba NR::amoeba(Mat_IO_DP &p, Vec_IO_DP &y, const DP
ftol, DP myfunk(Vec_I_DP &), int &nfunk), then it will not work.
How can I get around this? I don't want to change NR::amoeba. At this
moment, I declare Vec_I_DP y as a global variable. But I don't like
it.

"Dave" <> wrote in message
news:...
: I make this question clearer. Here is the routin in the NR
:
: void NR::amoeba(Mat_IO_DP &p, Vec_IO_DP &y, const DP ftol, DP
: funk(Vec_I_DP &), int &nfunk)
:
: You can see that DP funk(Vec_I_DP &) is a function call. Inside
: NR::amoeba, funk is called several times to do the calculation.
: The user are supposed to provide a function of this type. For example,
: the following;
:
: DP func(Vec_I_DP &x)
: {
: return 0.6-
: NR::bessj0(SQR(x[0]-0.5)+SQR(x[1]-0.6)+SQR(x[2]-0.7));
: }
:
: But my function needs another extra parameter, my function looks like
: this:
:
: DP myfunc(Vec_I_DP &x, Vec_I_DP &y)
: {
: return x[0]*y+x[1]*(y*y[i-1])+x[2]*exp(-y);
: }
:
: if I use NR::amoeba NR::amoeba(Mat_IO_DP &p, Vec_IO_DP &y, const DP
: ftol, DP myfunk(Vec_I_DP &), int &nfunk), then it will not work.
: How can I get around this? I don't want to change NR::amoeba. At this
: moment, I declare Vec_I_DP y as a global variable. But I don't like
: it.

You are right to dislike this approach -- but the poorly designed
NR::amoeba leaves you no choice.

To avoid this limitation, in C++, function objects should be used
instead of function pointers. See for example the 3-parameter
version of std::sort in the standard library, header <algorithm>.
Your preferred C++ book should explain how it is used.

In a C library, an alternative is to pass add additional "cookie"
parameter - an additional void* pointer that is passed to the
"engine" function, which the engine function passes through to
the callback.

The Numerical Recipes in C++ library suffers from being a
straightforward translation of the original Fortran code.
It is far from following good C++ design practices, and
would have to be heavily reworked to make it usable
in production C++ code...

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!