# Thread: pass extra data to a function

1. ## pass extra data to a function

Hi folks,
I'm working on a project hence in urgent need of guides. The problem might be clarified by using a simple example (if in need, I'll spell out the whole picture, however the following is the barebone):

suppose I want to minimize a function f(a), the argument for the function is only 'a' from the optimizor's point of view. Hence the optimizor wants to find the 'a' that will minimize f().

However, f(.) itself is not a simple function of 'a', it is derived out of function
g(a,b) where b is a constant determined during the runtime (i.e., you cannot declare f(.) by specifying 'b' beforehand). Worse, 'b' might be an array of two dimension (think of importing data into the function g(.) and construct f(.)).

I tried declaring b as an external, defining g(a,b), and then used g(.) in the definition of f(.), but failed. The linker said the 'b' is undeclared.

Please anyone helps me out of this. I appreciate it very much...

2. Could you... put some real code to that because it's pretty damn confusing as you state it. And what the hell is the difference between f() and f(.) ?

Edit: Also, what do you mean by "minimize" ? And what do you mean when you say a function is derived out of another.. ?

3. ## the code

I'm sorry; actually f() and f(.) are the same in my last thread.
Here's the code for illustration:

Code:
```#include "/*headfile, etc.*/"

double g(double &x, double *b) {
//b is to be a 2-dimension matrix
//x is a vector
....
}

double f(double &x) {
return g(x, &b);
}

int main() {

double x[dim]; //dim to be the dimension of x vector;

x=funmin(init_x, f); /* funmin() is a routine to minimize f() with the argument
vector init_x initialized for f()*/

}```
Please let me know if there's any question...

4. ## extern

I forgot to add, as a matter of fact, I declare both x and b to be external in the program; this is a bad design but I have no idea how to circumvent it...

5. I am posting this because you did not use code tags on this thread. In the furture please use Code Tags. They make your code MUCH easier to read and people will be much more likely to help you if you do. And they'll be happier about helping you

For example:

Without code tags:

for(int i=0;i<5;i++)
{
cout << "No code tags are bad";
}

With Code Tags:
Code:
```for(int i=0;i<5;i++)
{
cout << "This code is easy to read";
}```
This is of course a basic example...more complicated code is even easier to read with code tags than without.

I've added code tags for you this time. They can be added by putting [code] at the beginning of your code and [/code] at the end. More information on code tags may be found on the code tag post at the top of every forum. I also suggest you take a look at the board guildlines if you have not done so already.

This is a common mistake for those new to our forums, just remember to use [code] tags in the future and you'll get much more help.

If this is your first time posting here the welcome, and if there's anything I can do or any questions I can answer about these forums, or anything else, please feel free and welcome to PM me.

Kermi3

6. ## thanks

It's very kind of you for providing the info...

The linker gives the error message:
Code:
`undefined reference to 'b'.`
I'm using Bloodshed Dev C++...

8. What do you mean by minimize ? In four years of programming with C++ I've never seen such a term used anywhere in any book nor on any discussion board.

Edit: You haven't even declared 'b'. I guess the error message is meaningful.

9. Can you post a FULL WORKING EXAMPLE program (with comments, I/O and everything) to illustrate your problem? We really have no idea what you are talking about. I'm guessing it's rather trivial...

By the way, double &x is not a "vector", it's a regular double passed by reference. And where are you supplying the dimensions for the "two-dimensional array" b?

10. I declare 'b' by using EXTERN keyword, which I omit from my original code:
Code:
`extern double *b`
I'm thinking of a way to come up with a working example; my original code spans several files, not all of which are context-free...

11. Originally Posted by asmileguo
I declare 'b' by using EXTERN keyword, which I omit from my original code:
Code:
`extern double *b`
Originally Posted by asmileguo
The linker gives the error message:

Code:
`undefined reference to 'b'.`
I think if you have an "extern double * b;" declaration, you should also have somewhere a definition like "double * b;" (on file level, not inside a function), and also initialize it before calling your functions.

I hope it helps.

12. Do you mean optimize?

13. ## yes

I mean that by taking the minimum of a (mathmatical) function ( I realize it's slightly different from what we label as function in the programming...)

Originally Posted by Mario F.
Do you mean optimize?