Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > anyway to give extra parameter to an existing function?

Reply
Thread Tools

anyway to give extra parameter to an existing function?

 
 
Dave
Guest
Posts: n/a
 
      04-23-2007
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

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      04-23-2007
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.


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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
 
 
 
Default User
Guest
Posts: n/a
 
      04-23-2007
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.


Overload it.




Brian
 
Reply With Quote
 
Gianni Mariani
Guest
Posts: n/a
 
      04-23-2007
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.
>


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

way 2 is binary compatible if foo is in a library or such.
 
Reply With Quote
 
Dave
Guest
Posts: n/a
 
      04-23-2007
On Apr 23, 4:10 pm, Gianni Mariani <(E-Mail Removed)> wrote:
> 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.

>
> 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
> {}
>
> -------------------
>
> way 2 is binary compatible if foo is in a library or such.



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[i]+x[1]*(y[i]*y[i-1])+x[2]*exp(-y[i]);
}

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,

 
Reply With Quote
 
Ivan Vecerina
Guest
Posts: n/a
 
      04-24-2007
"Dave" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
: 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[i]+x[1]*(y[i]*y[i-1])+x[2]*exp(-y[i]);
: }
:
: 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
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Does return-by-value mean extra copies and extra overhead? mathieu C++ 3 09-04-2009 04:25 PM
Existing Dll - using Functions from an existing dll Tristin.Colby@gmail.com Ruby 0 02-05-2008 07:38 PM
Why no existing Java type to existing XML schema binding support? nrm Java 3 04-10-2006 04:52 PM
Give us 3 minutes; we give you the whole library lib Computer Support 0 01-27-2005 07:52 AM
Re: Anyway to copy a picture from a DVD extra JMKAUFFMAN DVD Video 9 07-07-2004 04:48 AM



Advertisments