473,395 Members | 1,856 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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.
Thanks,
Dave

Apr 23 '07 #1
5 3143
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
Apr 23 '07 #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. 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
Apr 23 '07 #3
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.
Apr 23 '07 #4
On Apr 23, 4:10 pm, Gianni Mariani <gi3nos...@mariani.wswrote:
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,

Apr 23 '07 #5
"Dave" <Da***********@gmail.comwrote in message
news:11**********************@n76g2000hsh.googlegr 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

Apr 24 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: peter | last post by:
Hello all, Recently I've started to refactor my code ...(I'm using python 2.3.4) I tried to add extra functionality to old functions non-intrusively. When I used a construct, which involves...
6
by: komal | last post by:
hi all basically my problem is i have to write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function...
11
by: Florian Loitsch | last post by:
I'm currently writing a JS->Scheme compiler (which, using Bigloo, automatically yields a JS->C, JS->JVM, JS->.NET compiler), and have a question concerning the function-parameters: According to...
17
by: abs | last post by:
My element: <span onclick="alert('test')" id="mySpan">test</span> Let's say that I don't know what is in this span's onclick event. Is it possible to add another action to this element's onclick...
0
by: Dino Hadzic | last post by:
Hallo csharp'ers I am trying to call a function on a COM (dll) object from managed c# code, which will send data to another node using an already existing bluetooth connection. However, I get a...
1
by: Trevor Morgan | last post by:
I've been having problems when a templated member function has the same name as ANY previously defined template parameter. Here's the simplest case that demonstrates the problem. This fails to...
2
by: saleek | last post by:
I was wondering if there is a way I can add an extra header to a datagrid? I found this solution on the internet - but it seems quite old and didn't work for me. ...
15
by: main() | last post by:
Hi all, When i compile following piece of code, # include <stdio.h> void fun(int val) { int val; /*problem is here*/ printf("%d\n",val);
7
by: ashtek | last post by:
Hi, I have a generic function that executes a stored procedure & returns a data table. Code: === public static DataTable ExecuteStoredProcedure(string strProc,SqlParameter paramArray) {...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.