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

  • Comp Sci
  • Thread starter trouty323
  • Start date
  • Tags
    C++ Function
In summary: Bisection (double, double, int, double, double (*)(double)...);/* function to call */In summary, In conclusion, 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.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
  • #1
trouty323
24
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
  • #2
You could just create two functions, the prototypes would be:

double f(double);
double g(double);
 
  • #3
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
Do you want to make a function and argument of another function? Something like being able to call bisection(f) and bisection (g)?
 
  • #5
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
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
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
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;
}
 
  • #9
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!
 

FAQ: How Do You Assign Functions to Variables in C++?

What is the syntax for assigning a function to a variable in C++?

The syntax for assigning a function to a variable in C++ is as follows:
data_type variable_name = function_name;
For example, if we have a function named add that takes in two integers and returns their sum, we can assign it to a variable named sum like this:
int sum = add;
This allows us to call the function using the variable name, like sum(5, 10), instead of using the function name directly.

Can a C++ function be assigned to multiple variables?

Yes, a C++ function can be assigned to multiple variables. This is useful when you want to call the same function using different names in your code. For example, if we have a function named calculate_area that calculates the area of a rectangle, we can assign it to two variables named get_area and rect_area like this:
double get_area = calculate_area;
double rect_area = calculate_area;
Both get_area and rect_area can now be used to call the calculate_area function.

Is it possible to assign a function to a variable without specifying its return type?

No, it is not possible to assign a function to a variable without specifying its return type in C++. This is because the compiler needs to know the return type of the function in order to correctly allocate memory and handle the function's return value. If the return type is not specified, the compiler will throw an error.

Can a C++ function have different return types when assigned to different variables?

No, a C++ function can only have one return type. When assigned to different variables, the function will still have the same return type. This is because the return type is part of the function's signature and cannot be changed by assigning it to different variables.

Can a function be assigned to a variable of a different type in C++?

No, a function cannot be assigned to a variable of a different type in C++. The variable and the function must have the same data type. Additionally, the variable must be a pointer or reference to a function in order for the assignment to be valid.

Similar threads

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