### Help with writing function

I am having some more troubles with functions. This function will calculate and return the X-coordinate of the vertex of the parabola. It takes two double arguments: the a-coefficient for the parabola and the b-coefficient for the parabola. It returns a double: the X-coordinate of the vertex of the parabola.

The formula for calculating the X-coordinate of the vertex is:

X-Vertex = -b-coefficient / ( 2 * a-coefficient)

`xVertex = calcXvertex( aCoeff, bCoeff );`

Here is what I have done so far (not sure if I'm right)
 ``123`` ``````double calcXvertex() calcXvertex = -bCoeff / (2 *aCoeff) xVertex = calcXvertex( aCoeff, bCoeff );``````
Thanks for the link (I read over it, and the C++ book), but that does not help me with the question above. I still have no clue how to tackle this problem.
 that does not help me with the question above
It should. The problem is that you're trying to define a function in a way that makes no syntactical sense.
ok, so something like this:

 ``123456`` ``````double calcXvertex ( double, double ); { xVertex = -bCoeff / ( 2 * aCoeff); return xVertex; }``````

The parameter names are not optional.

EDIT: Also, you're not declaring xVertex.
Last edited on
I declared xVertex prior to this chunk of code (sorry for not informing you).

I remember reading in the article that the parameter names are the values that will be involved in the function.

so I am just going to hit you with my code. So you can see the whole code (in full).
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146`` ``````#include #include #include using namespace std; //********************************************************************* //* //* Function Prototypes //* //********************************************************************* double getA (); double calcXvertex ( double, double ); double calcYvertex ( double, double, double ); void printConcavity ( double ); double calcDiscriminant ( double, double, double ); void printRoots ( double, double, double ); int main() { //********************************************************************* //* //* Variable Dictionary //* //* aCoeff, bCoeff, cCoeff - used to hold the coefficients //* //* xVertex - used to hold the X-coordinate of the vertex //* //* yVertex - used to hold the Y-coordinate of the vertex //* //********************************************************************* double aCoeff, bCoeff, cCoeff; double xVertex, yVertex; //Setup the formatting cout << fixed << setprecision(3); //Get the A, B, and C coefficients from the user double getA(); { cout << "Enter the a coefficient (non -zero value:\n"; cin >> aCoeff; while (aCoeff < 0) { cout << "Error: The a-value MUST be non-zero. Try Again:"; cin >> aCoeff; } return aCoeff; } //Calculate the X and Y coordinates for the vertex of the parabola double calcXvertex ( aCoeff, bCoeff ); { xVertex = -bCoeff / ( 2 * aCoeff); return xVertex; } double calcYvertex ( double, double, double ); { yVertex = ( aCoeff * (xVertex^2) ) + ( bCoeff * xVertex ) + cCoeff; return yVertex; }; //Display the coefficients and vertices cout << endl << "*******************************************" << endl << " Quadratic Equation Analyzer" << endl << "*******************************************" << endl << "a Coefficient" << setw(30) << aCoeff << endl << "b Coefficient" << setw(30) << bCoeff << endl << "c Coefficient" << setw(30) << cCoeff << endl << "-------------------------------------------" << endl << "X Coordinate" << setw(31) << xVertex << endl << "Y Coordinate" << setw(31) << yVertex << endl << "-------------------------------------------"; //Display the concavity of the parabola void printConcavity ( double ); { if ( aCoeff > 0 ) { cout << "Parabola opens UPWARD." << endl; } else { cout << "Parabola opens DOWNWARD." << endl; } return 0; } //Display the roots cout << endl << "-------------------------------------------"; double calcDiscriminant ( double, double, double ); { double discriminant; discriminant = (bCoeff^2 - 4) * (aCoeff * cCoeff); return discriminant; } void printRoots ( double, double, double ); { if ( calcDiscriminant > 0 ) { cout << "There are two real roots."; } if else ( calcDiscriminant == 0 ) { cout << "It has one root with a multiplicity of 2."; } else { cout << "There are no real roots"; } return 0; } cout << endl << endl; system("pause"); return 0; }``````

Do you think what I did in line 66 should be the status quo for every place on this script that has something like (double, double)? Also since I have given you the whole code, I might as well tell you that I have had some troubles compiling the thing, any clues why (except for the things you have already addressed)?

I really appreciated your help in dealing with me helios. I have spent hours writing this code, and I just want to go to bed feeling like I have accomplished something.
Last edited on
Okay, well, there's many things wrong, here.

1. You can't define functions inside of other functions. main() is a function.
2. Nearly every procedural programming language makes a distinction between formal parameters and actual parameters (look it up). I can very well do this:
 ``1234567`` ``````int times2(int x){ return 2*x; } //... int foo=5; int bar=times2(foo); foo=times2(bar);``````
There doesn't need to be any relation between the expression you pass to a function and the name of the corresponding formal parameter. In fact, most of the time there won't be.
What this means is that the parameter name, as I said before, is not optional.
There is one exception, though: if you don't use the parameter in the function, you can leave it anonymous:
 ``123`` ``````int foo(int){ return 2; }``````
The parameter type is always mandatory.
3. There are many semicolons where they don't belong. The semicolon terminates statements. If you misplace a semicolon, you can radically change the meaning of a piece of code:
 ``12345678910`` ``````int foo(int) { return 2; } [code]Means something very different from [code] int foo(int); { return 2; }``````
Depending on the surrounding context, one or the other may compile and the other not. However, you'll practically never want the latter version.
4. In C/++, ^ is not exponentiation.
5. Logic error: The discriminant is b*b - 4*a*c, not (b*b-4)*a*c.
6. `if else` is not a valid sequence of tokens. The valid form is `else if`
7. If a function has return type void, the return statement should either not be there, or not be followed by an expression.
 ``123456`` ``````void foo(){ } void bar(){ return; }``````

I'll clean the code up for you, so you can see how it should be done:
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889`` ``````#include #include #include using namespace std; // I'll omit the function declarations for stylistic reasons. Feel free to add // them back if you want. double getA(){ double aCoeff; cout << "Enter the a coefficient (non -zero value:\n"; cin >> aCoeff; while (aCoeff < 0){ cout << "Error: The a-value MUST be non-zero. Try Again:"; cin >> aCoeff; } return aCoeff; } double calcXvertex(double aCoeff,double bCoeff){ return -bCoeff / ( 2 * aCoeff); } double calcYvertex(double aCoeff,double bCoeff,double cCoeff){ double xVertex=calcXvertex(aCoeff,bCoeff); return aCoeff*xVertex*xVertex + bCoeff*xVertex + cCoeff; } void printConcavity(double aCoeff){ if (aCoeff>0) cout << "Parabola opens UPWARD." << endl; else cout << "Parabola opens DOWNWARD." << endl; } double calcDiscriminant(double a,double b,double c){ return b*b - 4*a*c; } void printRoots(double a,double b,double c){ //The function has to actually be called to take effect: double discrimitant=calcDiscriminant(a,b,c); if (discrimitant>0) cout << "There are two real roots."; else if (discrimitant==0) cout << "It has one root with a multiplicity of 2."; else cout << "There are no real roots"; } int main() { double aCoeff, bCoeff, cCoeff; double xVertex, yVertex; cout << fixed << setprecision(3); //The function has to actually be called to take effect: aCoeff=getA(); //TODO: get the other coefficients. xVertex=calcXvertex(aCoeff,bCoeff); yVertex=calcYvertex(aCoeff,bCoeff,cCoeff); //Display the coefficients and vertices cout << endl << "*******************************************" << endl << " Quadratic Equation Analyzer" << endl << "*******************************************" << endl << "a Coefficient" << setw(30) << aCoeff << endl << "b Coefficient" << setw(30) << bCoeff << endl << "c Coefficient" << setw(30) << cCoeff << endl << "-------------------------------------------" << endl << "X Coordinate" << setw(31) << xVertex << endl << "Y Coordinate" << setw(31) << yVertex << endl << "-------------------------------------------"; //Once again, the function has to be called, or nothing happens. printConcavity(aCoeff); cout << endl << "-------------------------------------------"; printRoots(aCoeff,bCoeff,cCoeff); cout << endl << endl; return 0; }``````
Last edited on
Thank you helios, I am left speechless. I took 30min+ to read this code over and over again to insure that I did not waste your time in teaching me. I never knew that functions could even be placed before main() like that.

I only have one thing to say: YOU ARE THE BEST :).
Topic archived. No new replies allowed.