anyway to give extra parameter to an existing function?

D

Dave

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.


Thanks,
Dave
 
V

Victor Bazarov

Dave said:
I am using some recipes to do some calculation. But there are
situation that I need to give extra parameters to the existing
function.

What for?
For example, the recipe has a function called:
foo (int x, int y)
But now I want to give another parameter, say, float z.

Why? What does the function need it for?
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.

What's inelegant about changing the function?
Because I
don't want to mess up the existing library/functions.

If the function isn't changed (messed up), what is the point of
giving it an extra argument? If the argument is not used, why
bother passing it in?

V
 
D

Default User

Dave said:
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.

Overload it.




Brian
 
G

Gianni Mariani

Dave said:
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.

two ways :

---- way 1 --------
void foo( int x, int y, int z = 0 ) // add a new default parameter
{
}

---- way 2 --------

void foo( int x, int y )
{
}

void foo( int x, int y, int z ) // overload foo with 3rd paramteter
{
}
 
D

Dave

two ways :

---- way 1 --------
void foo( int x, int y, int z = 0 ) // add a new default parameter
{

}

---- way 2 --------

void foo( int x, int y )
{

}

void foo( int x, int y, int z ) // overload foo with 3rd paramteter
{}


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.

Thanks,
 
I

Ivan Vecerina

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


I hope this helps,
Ivan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top