Comp Sci How Do You Assign Functions to Variables in C++?

  • Thread starter Thread starter trouty323
  • Start date Start date
  • Tags Tags
    C++ Function
AI Thread Summary
To assign functions to variables in C++, one can use function pointers, allowing for cleaner code when implementing methods like the bisection method. Users can define functions for f(x) and g(x) and pass them as parameters to a bisection function, which can then call the appropriate function based on the pointer provided. This approach avoids code duplication by enabling the same bisection routine to handle different functions. The key is to ensure that the function signatures match and that the return types and parameters are consistent. This method streamlines the process of computing roots for multiple functions efficiently.
trouty323
Messages
23
Reaction score
0
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.
 
Physics news on Phys.org
You could just create two functions, the prototypes would be:

double f(double);
double g(double);
 
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.
 
Do you want to make a function and argument of another function? Something like being able to call bisection(f) and bisection (g)?
 
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.
 
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.
 
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.
 
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:
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:
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;
}
 
I appreciate all of your guy's input. I just have a hard time understanding it if it's not shown to me.
 
  • #10
You could define your own function and then call it from your bisection routine.

Code:
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:
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
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
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
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:
/* 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:
  • #14
rcgldr said:
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:
/* 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);
/* ... */
}

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

Similar threads

Replies
1
Views
2K
Replies
2
Views
2K
Replies
8
Views
2K
Replies
15
Views
2K
Replies
1
Views
3K
Replies
23
Views
3K
Replies
3
Views
2K
Back
Top