anyway to give extra parameter to an existing function?

Discussion in 'C++' started by Dave, Apr 23, 2007.

  1. Dave

    Dave Guest

    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
    Dave, Apr 23, 2007
    #1
    1. Advertising

  2. 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
    Victor Bazarov, Apr 23, 2007
    #2
    1. Advertising

  3. Dave

    Default User Guest

    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
    Default User, Apr 23, 2007
    #3
  4. 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.
    Gianni Mariani, Apr 23, 2007
    #4
  5. Dave

    Dave Guest

    On Apr 23, 4:10 pm, Gianni Mariani <> 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+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,
    Dave, Apr 23, 2007
    #5
  6. "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...


    I hope this helps,
    Ivan
    --
    http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
    Brainbench MVP for C++ <> http://www.brainbench.com
    Ivan Vecerina, Apr 24, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. nrm
    Replies:
    3
    Views:
    525
  2. komal
    Replies:
    6
    Views:
    1,418
    msalters
    Jan 25, 2005
  3. grocery_stocker
    Replies:
    10
    Views:
    620
    Keith Thompson
    May 25, 2005
  4. mathieu
    Replies:
    3
    Views:
    593
    Bo Persson
    Sep 4, 2009
  5. AzamSharp
    Replies:
    2
    Views:
    174
Loading...

Share This Page