Topic: C Function Area of Rectangle

Write a C++ function to calculate the area when height and widthÂ are given as arguments. The function will accept 2 double parameters height and width of type double and return a double(area of the rectangle).

Hint: area = height x width

## Definition of C Function Area of Rectangle

First of all we will define a function that will take two arguments of type double : height and width of rectangle.

This function will return a double value that is the area of the rectangle obtained by multiplying the given height and width.

double findArea (double height, double width) { return height*width; }

### Calling the findArea() function

we can call a function that will return a value either in an assignement statement or in a printf() function. So that the returned value can be stored or used directly.

variable = function(arg1, arg2);

For example,

a = findArea(h,w);

where h and w are two variables with the given values of height and width of the rectangle. The function findArea() will calculate and return the area of rectangle which is stored in the variable a on the left side of the assignment statement.

We may print the value of the variable ‘a’.

printf("The area of Rectangle is %.2lf",findArea(h,w));

Source code

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include<stdio.h> double findArea (double height, double width) { return height*width; } int main() { double h,w; printf("Enter the height of the rectangle: "); scanf("%lf",&h); printf("Enter the width of the rectangle: "); scanf("%lf",&w); printf("The area of Rectangle is %.2lf",findArea(h,w)); return 0; } |

Output: Enter the height of the rectangle: 6 Enter the width of the rectangle: 9 The area of Rectangle is 54.00