 # C++ Program to find the area and perimeter of a rectangle using function.

In this example program, you will learn and get code on finding the area and perimeter of a rectangle in C++ programming. Here we are using the approach using class and object to create the program.

## How to calculate the area and perimeter of a rectangle?

The area of a rectangle is the region occupied by a rectangle within its four sides or boundaries.
The area of a rectangle depends on its sides. The formula for area is equal to the product of the length and breadth of the rectangle.
Area of rectangle = Length x Breadth
A = lb

The perimeter of a rectangle is the total distance covered by its boundaries or the sides. Since there are four sides of a rectangle, thus, the perimeter of the rectangle will be the sum of all four sides.
Since the perimeter is equal to the sum of all the sides of the polygon. Hence, in the case of a rectangle, the perimeter (P) is;
P = sum of all its four sides
P = a + b + a + b   (Opposite sides of rectangle are equal)
P = 2(a + b).

## C++ Functions

A Function is a sub-program that acts on data and often returns a value. Large programs are generally avoided because it is difficult to manage a single list of instructions. Thus, a large program is broken down into smaller units known as functions. A function is a named unit of a group of program statements. This unit can be invoked from other parts of the program.
In C++, a function must be defined before it is used anywhere in the program.
Here is the general form to define a function in C++
```return_type function_name(comma_separated_parameter_list) {    function body }```
A function is called or invoked or executed, by providing the function name, followed by the parameters being sent enclosed in the parentheses.

## C++ program to calculate the area and perimeter of a rectangle using function.

Ask the user to enter the value of the length and breadth of the rectangle. Read the value into the float type variables l and b.define a function to compute the area of the rectangle.
```float areaofrectangle(float l, float b) {     return (l*b); }```
Float – return type
areaofrectangle – function name
float l, float b – parameters
The function will return the value l * b.
Get the value to the variable area
area = areaofrectangle(l, b);
Display the result.
Define another function to compute the perimeter of the rectangle with the value entered into the variables l and b.
```float perimeterofrectangle(float l, float b) {     return (2 * (l + b)); }```
float – return type
perimeterofrectangle – function name
float l, float b – parameters
The function will return the value (2 * (l + b)).
Get the value to the variable perimeter
perimeter = perimeterofrectangle(l, b);

Display the result

### Algorithm

Step 1:  Call the header file `iostream.`

Step 2: Use the `namespace std.`

Step 3: Declare two functions for compute the area and the perimeter of the rectangle;

float areaofrectangle( float l, float b);

float perimeterofrectangle( float l, float b);

Step 4: Open the integer type main function; int main().

Step 5: Declare  float type variables l, b, area, perimeter;

Step 6: Ask the user to enter the value of length and breadth of the rectangle.

Step 7: Get the value of length to the variable l and breadth to b;

Step 8: Call step: 12 and get the value to the variable area;

Step 9: Display the result

Step 10: Call step: 13 and get the value to the variable perimeter;

Step 11: Display the result;

Step 12: Define the function float areaofrectangle( float l, float b);

`float areaofrectangle(float l, float b) { return (l*b); }`

Step 13: Define the function float perimeterofrectangle( float l, float b);

```float perimeterofrectangle(float l, float b)           {  return (2 * (l + b));  }```

Step 14: Exit

## C++ Source Code

```                                          ```#include<iostream>
using namespace std;
float areaofrectangle(float, float);
float perimeterofrectangle(float, float);
int main()
{
float l, b, area, perimeter;
cout<<"Enter Length of Rectangle: ";
cin>>l;
cin>>b;
area = areaofrectangle(l, b);
perimeter = perimeterofrectangle(l, b);
cout<<"\nArea = "<<area;
cout<<endl;
cout<<"\nPerimeter = "<<perimeter;
cout<<endl;
return 0;
}
float areaofrectangle(float l, float b)
{
return (l*b);
}
float perimeterofRectangle(float l, float b)
{
return (2*(l+b));
}```
```

`234`
VIEW ALL
VIEW ALL
VIEW ALL