Modularizing and Reusing of code through Functions
Calculation
of the area of a circle is separated into a separate module, from the
calculation of an area of a ring, and the same module can be reused multiple
times.
/* Program to find area of a ring */
#include<stdio.h>
int
main()
{
float
a1,a2,r1,r2;
printf(“enter
the radius:”);
scanf(“%f”,&r1);
a1=3.14*r1*r1;
printf(“enter
the radius:”);
scanf(“%f”,&r2);
a2=3.14*r2*r2;
a=a1-a2;
printf(“Area
of ring: %3f\n”,a);
}
To
understand, modularization and reusing of code through functions, let us
consider the following program
Calculation
of an area of a Circle from the calculation of an area of the ring and the same
module can be reused a multiple times.
The
calculation of an area of a ring, using repeated and reusable blocks of code is
shown on the screen.
Here,
blocks of code highlighted on the screen are used to find the areas of two
circles from which the area of the ring is calculated as ‘a1-a2’ which is
stored in ‘a’ and returned.
Modularizing and Reusing of code through Functions |
Modularizing and Reusing of code through Functions |
x