Function Returns

Hello,

I want to be able to change my code so that each function will return the respected calculation (i.e. rect_area will return the area). How would I go about doing so?

All help is appreciated!

Thank you,

Huppa

*I also wanted to add if there is anything wrong with my current code, please tell me, I am new to functions as a whole and they are not my strong suit at the moment.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream.h>

   void rect_area(float, float);
   void rect_peri(float, float);
   void rect_vol(float, float, float);
           
   void main (void)
           
   
   {
   
      float length, width, height;
   
      cout<<"What is the length of the rectangle?\n";
      cin>>length;
      cout<<"What is the width of the rectangle?\n";
      cin>>width;
      cout<<"What is the height of the rectangle?\n";
      cin>>height;
      rect_area(length, width);
      rect_peri(length, width);
      rect_vol(length, width, height);
   
   }

           
   void rect_area(float L, float W)
           
   {
      float area;
      area=(L*W);
      cout<<"The area of the rectangle is: "<<area<<endl;
   }

           
   void rect_peri(float L, float W)
           
   {
      float peri;
      peri=((2*L)+(2*W));
      cout<<"The perimeter of the rectangle is: "<<peri<<endl;
   }


           
   void rect_vol (float L, float W, float H)
           
   {
      float vol;
      vol=(L*W*H);
      cout<<"The volume of the rectangle is: "<<vol<<endl;
   }
Last edited on
Instead of being a void function, the function would return a float value. Change that in both the function prototype and the function definition.

And you can just have one statement which returns the value of the calculation, e.g. for area...

return L*W;

Then in main - you can put the function call into a cout statement if you just want to print the value.

cout << rect_area(length, width);
Last edited on
@wildblue,

Thank you very much! Everything seems to be working now. :)
What compiler are you using? The current standard doesn't require C++ Standard Library headers to have the .h ending, main must return int, and the C++ Standard Library is in the std namespace so you either have to have std:: in front or do like I did below (which is considered bad by some) and do using namespace std; under the includes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
   using namespace std;

   float rect_area(float, float);
   float rect_peri(float, float);
   float rect_vol(float, float, float);
           
   int main ()   
   {
   
      float length, width, height;
   
      cout<<"What is the length of the rectangle?\n";
      cin>>length;
      cout<<"What is the width of the rectangle?\n";
      cin>>width;
      cout<<"What is the height of the rectangle?\n";
      cin>>height;
      cout << "Area: " << rect_area(length, width) << endl;
      cout << "Perimeter: " << rect_peri(length, width) << endl;
      cout << "Volume: " << rect_vol(length, width, height) << endl;
   
   }

           
   float rect_area(float L, float W)
           
   {
     return L*W;
   }

           
   float rect_peri(float L, float W)
           
   {
      return ((2*L)+(2*W));
   }


           
   float rect_vol (float L, float W, float H)
           
   {
      return L*W*H;
   }
Last edited on
Topic archived. No new replies allowed.