### Value-Returning Function not working?

For some reason i keep getting a error for line 29 saying Error expected a ";" and I don't understand what the problem is. The purpose of the program is to display a menu, let the user choose and then display the area's of the shape chosen using a value-return function.

 ``12345678910111213141516171819202122232425262728293031323334353637383940`` `````` #include //cout and cin #include //getch #include //used for better format using namespace std; //Function Declaration: double AreaofRectangle(int width, int height) int main() { //Constants for the Menu const int RECTANGLE_CHOICE = 1; const int TRIANGLE_CHOICE = 2; const int CIRCLE_CHOICE = 3; //Variables int choice; //Display Menu cout <<"\n\tArea Calculator Menu\n\t" << endl; cout << "1.Rectangle" << endl << "2.Triangle" << endl << "3.Circle" << endl << "Enter in your choice:" << endl; cin >> choice; if(choice == 1) { double AreaofRectangle(int width, int height) { //Problem Occurs here it says that it expects a ;// double resultRect; resultRect = width * height; return resultRect; } } } ``````
Last edited on
Well it is looking at line 28 and expecting that to be a call to a function... that function doesn't exist but besides that point.
Last edited on
You must review and complete your code. Here are my suggestions for now:
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051`` ``````#include //cout and cin #include //getch #include //used for better format using namespace std; //Function Declaration: double AreaofRectangle(double, double); int main() { //Variables int choice; double height, width; //Stuff for Triangle and Circle: base, radius... //Display Menu cout <<"\n\tArea Calculator Menu\n\t" << endl; cout << "1.Rectangle" << endl << "2.Triangle" << endl << "3.Circle" << endl << "Enter in your choice:" << endl; cin >> choice; if(choice == 1) { cout << "Rectangle: enter width and height: \n"; cout << "Width: "; cin >> width; cout << "Height: "; cin >> height; cout << "RectangleArea = " << AreaofRectangle(width, height); } /* else if(choice == 2) { //Code for Triangle } else if(choice == 3) { //Code for Circle } */ getch(); return 0; } double AreaofRectangle(double width, double height) { return width * height; }`````` ``` Area Calculator Menu 1.Rectangle 2.Triangle 3.Circle Enter in your choice: 1 Rectangle: enter width and height: Width: 123 Heighth: 321 RectangleArea = 39483```
Last edited on
Thanks for the reply, when I use your modifications I can't insert a do while loop, and isn't the does the value-return function create variable double AreaofRectangle and double width, double height?
Last edited on
line 31 needs a semicolon:
change
`double AreaofRectangle(int width, int height)`

to

`double AreaofRectangle(int width, int height);`
canucksfan1

`What do you mean by in line 16 //stuff for triangle, circle? `

stuff for triangle and circle: of course the data for these shapes: the base and height of the triangle, circle radius, #pi...what else?! :D...
For area circle and triangle and rectangle I have to use a value-return function, and I don't know what they are used for
OK! Then in those functions must be entered those data concerning rectangle, triangle and circle as I said before despite you have to use a value-return function!..I've seen in your code `double AreaofRectangle(int width, int height) you don't enter those values but ` you pass them from nowhere! That's all!
Hi there,

Let's clear a few things up (I'm using your original code):

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647`` `````` #include //cout and cin #include //getch #include //used for better format using namespace std; //Function Declaration: double AreaofRectangle(int width, int height); //<- need a semicolon here, syntax error int main() { //Constants for the Menu const int RECTANGLE_CHOICE = 1; const int TRIANGLE_CHOICE = 2; const int CIRCLE_CHOICE = 3; //Variables int choice; //Display Menu cout <<"\n\tArea Calculator Menu\n\t" << endl; cout << "1.Rectangle" << endl << "2.Triangle" << endl << "3.Circle" << endl << "Enter in your choice:" << endl; cin >> choice; if(choice == 1) { //Can't define a function within another function (main() in this case) double AreaofRectangle(int width, int height) { //Problem Occurs here it says that it expects a ;// double resultRect; resultRect = width * height; return resultRect; } } } //so move it here double AreaofRectangle(int width, int height) { double resultRect; resultRect = width * height; return resultRect; }``````

I think a small explanation of the difference between declaration is in order:

 ``78`` ``````//Function Declaration: double AreaofRectangle(int width, int height);``````

This tells the compiler "Hey compiler, please take note that I intend to use a function called AreaofRectangle, which takes two ints as arguments, the definition of what it does will follow later, but in the meanwhile, don't freak out if you encounter the name "AreaofRectangle", mkay?".

 ``123456`` `````` double AreaofRectangle(int width, int height) { //Problem Occurs here it says that it expects a ;// double resultRect; resultRect = width * height; return resultRect; }``````

This is the definition, it tells the compiler what your function actually does, so it can make sure this code is performed the function is called.

On a small sidenote, `switch` statements are usually preferred for these kinds of menu's, because they make these things more readable:

 ``1234567891011121314151617`` ``````switch (choice) { case 1: rectangle_processing(); break; case 2: square_processing(); break; case 3: triangle_processing(); break; default: //any input we did not expect //reprint menu and /or error message }``````

Value return functions are, well, functions which return a value. In your example:

 ``123456`` ``````double AreaofRectangle(int width, int height) { double resultRect; resultRect = width * height; return resultRect; }``````

This function returns a double, so you could do, for instance:
`double result = AreaofRectangle(5, 10);`
result will be assigned the value returned by the function.

Value return functions are opposed to `void` functions, which just "do stuff", without returning any results. Note that you could call return in a void function, but it will just end the function, rather than return an actual value. An example of a void function which could be applicable to your program:

 ``1234567891011`` ``````void process_rectangle() { int width, height; cout << "Rectangle: enter width and height: \n"; cout << "Width: "; cin >> width; cout << "Height: "; cin >> height; cout << "RectangleArea = " << AreaofRectangle(width, height); };``````

Hope that makes sense and helps, please do let us know if you require any further help.

All the best,
NwN
Last edited on
Topic archived. No new replies allowed.