# Homework Help: C++ assign function to varible

1. Feb 8, 2012

### trouty323

Hello everyone. I have an assignment that is asking me to find the roots using the bisection method. We are to find roots for two different functions.

f(x) = x3 + 3x - 1

and

g(x) = x3 - 2sin(x)

I was wondering if there is any way to assign these functions to the variables f and g, respectively. By doing this, I could shorten my code greatly. Thanks in advance.

2. Feb 8, 2012

### rcgldr

You could just create two functions, the prototypes would be:

double f(double);
double g(double);

3. Feb 8, 2012

### MisterX

Is a defining them as functions f and g not acceptable? I'm not sure how advanced is your knowledge of C++.

Do you want to be able to write f and g instead of f(x) and g(x)? I'm not sure if instead you wanted f(x) instead of x*x*x + 3*x - 1 or something.

4. Feb 8, 2012

### Staff: Mentor

Do you want to make a function and argument of another function? Something like being able to call bisection(f) and bisection (g)?

5. Feb 8, 2012

### trouty323

Basically, I do not want to reuse code that only needs one thing changed. I want to have one function with one equation, and one function with the other equation. I would have these functions set aside and in my function where I compute the roots, I would call the function and it automatically goes to the correct equation.

6. Feb 8, 2012

### trouty323

The way my book describes it seems like they are assigning two whole functions to two separate variables. Then, when I call both functions in my main function, I can easily use one variable, let's say f for both, to go to the right function. Sorry if that seems confusing, I am sort of a beginner.

7. Feb 8, 2012

### Staff: Mentor

In C and C++, the name of a function variable is a pointer to the address in memory of the body of the function. As long as you declare a function correctly (as in rcgldr's post), you can assign new values to it to your heart's delight. The only restriction is that the return types have to be the same, and the types of the parameter have to be the same.

8. Feb 8, 2012

### trouty323

This is not my whole code, but it's really the only part that matters. It may be easier for you to see it than for me to try to explain it to you. The section right below me is called from my main function. I also want to be able to call this same function from main right after this one is computed, only compute with a different equation. As you can tell, the "fStart =" and "fEnd =" is specific to one equation, and I want to do something like "fStart = equationFunction(xStart)" and it will choose when equation to plug it into.

Code (Text):

void fBisection (double xStart, double xEnd, int nMax, double e)
{
double xMiddle, fStart, fMiddle, fEnd, error;

// Find starting and ending function values
fStart = pow(xStart, 3) + (3 * xStart) - 1;
fEnd = pow(xEnd, 3) + (3 * xEnd) - 1;

This is my main function. As you can tell, I am calling an fBisection and gBisection, which is how I want it, but I want to be able to store a function in f and a function in g and pass them through when I call each respective function. Then, when the code above receives the argument, it will easily know which equation I am talking about.

Code (Text):

int main ()
{
int nMax = 25;
double xStart, xEnd, e = 5.0e-7;

xStart = 0;
xEnd = 1;
// Call first bisection function
fBisection (xStart, xEnd, nMax, e);

cout << endl << endl;

xStart = .5;
xEnd = 2;
// Call second bisection function
gBisection (xStart, xEnd, nMax, e);

system("pause");
return 0;
}

9. Feb 8, 2012

### trouty323

I appreciate all of your guy's input. I just have a hard time understanding it if it's not shown to me.

10. Feb 8, 2012

### Staff: Mentor

You could define your own function and then call it from your bisection routine.

Code (Text):
double f(double arg)
{
return arg*arg*arg + 3.0 * arg - 1.0;
}
I tend to not use pow() if I can get away with not using it, since multiplication is much faster than using the pow() function.

The function above would be called like so:
Code (Text):
void fBisection (double xStart, double xEnd, int nMax, double e)
{
double xMiddle, fStart, fMiddle, fEnd, error;
// Find starting and ending function values
fStart = f(xStart);
fEnd = f(xEnd);
.
.
.

11. Feb 8, 2012

### trouty323

I realize that I could do that, but wouldn't I then have to create a separate routine for g(x)? I'm trying not to duplication the routine.

12. Feb 8, 2012

### Staff: Mentor

Well, of course, but how difficult is it to do that? It would be nearly the same as what I have for f, with the only difference being in the name (g) and the function body.

13. Feb 8, 2012

### rcgldr

You could use a pointer to function as a parameter, which effectively assigns a function to a variable (the pointer to function).

The code would look like this:

Code (Text):

/* prototypes */
double f(double);
double g(double);
void fBisection (double, double, int, double, double (*)(double) );
/* ... */

/* call using f() */
fBisection (xStart, xEnd, nMax, e, f);
/* ... */

/* call using g() */
fBisection (xStart, xEnd, nMax, e, g);
/* ... */

/* fBisection() partial code */
void fBisection (double xStart, double xEnd, int nMax,
double e, double (*pf)(double))
{
/* ... */
fStart = pf(xStart);
/* ... */
}

Last edited: Feb 8, 2012
14. Feb 9, 2012

### trouty323

Thank you very much! That helped a lot. Also, thanks to everyone else. Greatly appreciated!