Using a Function to Return Area, Perimeter

Page 1 of 1

6 Replies - 4468 Views - Last Post: 15 April 2009 - 07:20 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=99624&amp;s=ea574d95d6b8368c0aea1ad451a02d93&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 smms25

Reputation: 0
• Posts: 20
• Joined: 02-March 09

Using a Function to Return Area, Perimeter

Posted 15 April 2009 - 06:52 PM

okay , so i have no clue how to use a function in c++, this program is supposed to ask for the length and width of the rectangle (this is done in the main function... i used cin>>length,etc.) .... the function should return area and perimeter of the rectangle.... any help appreciated

```#include "stdafx.h"
#include "iostream"
using namespace std;

double areaRectangle (double length, double width);
void perimfRectangle (double length, double width);

int main(void)
{
double length,width,area,perimeter;
cin>>length;
cin>>width;
area=areaRectangle(double length, double width);
cout<<"\n Area of Rectangle="<<area;
perimfrectangle(double length, double width);
cout<<"\n Perimeter of Rectangle="<<perimf;
cout<<endl;
cin.get();
return 0;
}

double areaRectangle(double length,double width);
{
return length*width;
}
void perimfRectangle(double length,double width);
{
return (2*length+2*width);
}

```

This post has been edited by smms25: 15 April 2009 - 06:54 PM

Is This A Good Question/Topic? 0

Replies To: Using a Function to Return Area, Perimeter

#2 BetaWar

• #include "soul.h"

Reputation: 1647
• Posts: 8,510
• Joined: 07-September 06

Re: Using a Function to Return Area, Perimeter

Posted 15 April 2009 - 07:01 PM

Well, you have a few errors.

First:
```void perimfRectangle(double length,double width);
{
return (2*length+2*width);
}

```

can only return void, as it has void as the return type. Returning anything other than void will cause an error.

You will need to change it to something like so:
```double perimfRectangle(double length,double width);
{
return (2*length+2*width);
}

```

and remember to update your function definition above main as well.

Second, functions are case sensitive.
```	perimfrectangle(double length, double width);
```

will not work.

Third, primf is never declared.
```	cout<<"\n Perimeter of Rectangle="<<perimf;
```

So you are trying to output a variable that was never created.

So there are a few things to fix, then it should act better.

HTH

#3 BlakeJustBlake

• D.I.C Regular

Reputation: 26
• Posts: 441
• Joined: 15-February 09

Re: Using a Function to Return Area, Perimeter

Posted 15 April 2009 - 07:02 PM

On the top, take out the lengths and widths and leave the doubles. In main take out the doubles and leave the lengths and widths. At the bottom, leave it like it is.

#4 smms25

Reputation: 0
• Posts: 20
• Joined: 02-March 09

Re: Using a Function to Return Area, Perimeter

Posted 15 April 2009 - 07:11 PM

okay so i fix some of the errors that the c++ gave back however, im getting this error--- '{' : missing function header (old-style formal list?), where
```double areaRectangle(double length,double width);
{
return length*width;}
double perimf(double length,double width);
{
return (2*length+2*width);
}
```

as well as error C2064: term does not evaluate to a function taking 2 arguments
this error when i declare perimeter=...
---
```#include "stdafx.h"
#include "iostream"
using namespace std;

double areaRectangle (double length, double width);
double perimf(double length, double width);

int main(void)
{
double length,width,area,perimeter,perimf;
cin>>length;
cin>>width;
area=areaRectangle(length,width);
cout<<"\n Area of Rectangle="<<area;
perimeter=perimf(length,width);
cout<<"\n Perimeter of Rectangle="<<perimf;
cout<<endl;
cin.get();
return 0;
}

double areaRectangle(double length,double width);
{
return length*width;}
double perimf(double length,double width);
{
return (2*length+2*width);
}
```

#5 BetaWar

• #include "soul.h"

Reputation: 1647
• Posts: 8,510
• Joined: 07-September 06

Re: Using a Function to Return Area, Perimeter

Posted 15 April 2009 - 07:17 PM

Look at the variables you are declaring:
```double length,width,area,perimeter,perimf;
```

And on this line:
```	cout<<"\n Perimeter of Rectangle="<<perimf;
```

You are using perimf as a function as a variable. You can't have a variable and function, or class, or struct with the same name.

Just delete the variables perimf, and it should be happier.

HTH

#6 BlakeJustBlake

• D.I.C Regular

Reputation: 26
• Posts: 441
• Joined: 15-February 09

Re: Using a Function to Return Area, Perimeter

Posted 15 April 2009 - 07:19 PM

The first one is because you have semicolons after your parameters on those functions, there shouldn't be.

The second one is because in main you declare a double called perimf, and you can't overload names like that.

#7 smms25

Reputation: 0
• Posts: 20
• Joined: 02-March 09

Re: Using a Function to Return Area, Perimeter

Posted 15 April 2009 - 07:20 PM

smms25, on 15 Apr, 2009 - 06:11 PM, said:

okay so i fix some of the errors that the c++ gave back however, im getting this error--- '{' : missing function header (old-style formal list?), where
```double areaRectangle(double length,double width);
{
return length*width;}
double perimf(double length,double width);
{
return (2*length+2*width);
}
```

as well as error C2064: term does not evaluate to a function taking 2 arguments
this error when i declare perimeter=...
---
```#include "stdafx.h"
#include "iostream"
using namespace std;

double areaRectangle (double length, double width);
double perimf(double length, double width);

int main(void)
{
double length,width,area,perimeter,perimf;
cin>>length;
cin>>width;
area=areaRectangle(length,width);
cout<<"\n Area of Rectangle="<<area;
perimeter=perimf(length,width);
cout<<"\n Perimeter of Rectangle="<<perimf;
cout<<endl;
cin.get();
return 0;
}

double areaRectangle(double length,double width);
{
return length*width;}
double perimf(double length,double width);
{
return (2*length+2*width);
}
```

sorry for the extra post , working on my issues now