C++ Program to Find Area of Rectangle Using Function

Howdy readers, today you will learn how to write a program to find the area of a triangle using function in C++ Programming language.

C++ Program to Find Area of Rectangle Using Function

The below program prompts the user to enter the dimensions of the rectangle, then we define a user-defined function which calculates and returns the area of a rectangle.

Area of a rectangle is the product of its length and breadth. We will keep this fact in mind while writing the program.

So, without any delay, let’s begin this tutorial.

C++ Program to Find Area of Rectangle Using Function

C++ Program

// C++ Program to Find Area of Rectangle Using Function
#include <iostream>
using namespace std;

int areaRect(int x, int y){
    return x * y;
}

int main(){
    int length, breadth, area;
    
    // Taking input from the user
    cout << "Enter the length: ";
    cin >> length;
    cout << "Enter the breadth: ";
    cin >> breadth;
    
    // Calling out user-defined function
    area = areaRect(length, breadth);
    
    // Displaying result
    cout << "Area of the rectangle is: " << area << endl;
    
    return 0;
}

Output

Enter the length: 5
Enter the breadth: 7
Area of the rectangle is: 35
C++ Program to Find Area of Rectangle Using Function

Explanation

int areaRect(int x, int y){
    return x * y;
}

In the above program, we have defined a user-defined function named areaRect which calculates the area of the rectangle when two arguments are passed. The two arguments should be the dimensions of the rectangle.

    int length, breadth, area;

In the main function, we have declared three int data type variables named length, breadth and area.

    // Taking input from the user
    cout << "Enter the length: ";
    cin >> length;
    cout << "Enter the breadth: ";
    cin >> breadth;

This program prompts the user to enter the length and breadth of the rectangle.

    // Calling out user-defined function
    area = areaRect(length, breadth);

After that, the user-defined function is called in the main function. This gives us the area of the rectangle. This value gets stored in the area named variable.

    // Displaying result
    cout << "Area of the rectangle is: " << area << endl;

The area of the rectangle computed using a function is displayed on the screen using the cout statement.

Conclusion

I hope after reading this post, you understand how to write a program to find the area of a rectangle using function in C++ Programming language.

If you have any queries regarding the program, then let us know in the comment section. We will be pleased to solve all of your queries.

Leave a Comment