# unit2_2 - Modularizing and Reusing of code through...

This preview shows pages 1–3. Sign up to view the full content.

/* program to find area of a ring */ #include<stdio.h> int main() { float a1,a2,a,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); } /* program to find area of a ring */ #include<stdio.h> float area(); int main() { float a1,a2,a; a1 = area(); a2 = area(); a = a1- a2; printf("Area of Ring : %.3f\n", a); } float area() { float r; printf("Enter the radius : "); scanf("%f", &r); return (3.14*r*r); } Modularizing and Reusing of code through Functions Calculation of area of Circle is separated into a separate module from Calculation of area of Ring and the same module can be reused for multiple times. Function Declaration Function Definition Function Calls Repeated & Reusable blocks of code

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
A Function is an independent, reusable module of statements, that specified by a name. This module (sub program) can be called by it’s name to do a specific task. We can call the function, for any number of times and from anywhere in the program. The purpose of a function is to receive zero or more pieces of data, operate on them, and return at most one piece of data. A Called Function receives control from a Calling Function . When the called function completes its task, it returns control to the calling function. It may or may not return a value to the caller. The function main() is called by the operating system; main() calls other functions. When main() is complete, control returns to the operating system. int main() { int n; float p, r, si; printf(“Enter Details of Loan1:“); scanf( “%f %d %f”, &p, &n, &r); si = calcInterest ( p, n , r ); printf(“Interest : Rs. %f”, si); printf(“Enter Details of Loan2:“); } float calcInterest (float loan , int terms , float iRate ) { float interest;
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 11/15/2011 for the course ECKOW 123 taught by Professor Riuyt during the Spring '11 term at University of Damascus.

### Page1 / 9

unit2_2 - Modularizing and Reusing of code through...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online