Thursday, March 3, 2011

User-Defined Functions

The program presented previously now we modify the function value, to return the final amount calculated to the main, which will display the required output at the terminal. Also extend the versatility of the function printline by having it take the length of the line as an argument.

#include <stdio.h>
void printline (char ch, int len);
     value (float, float, int);
void main( )
{
 float principal, inrate, amount;
 int period;
 printf("Enter principal amount, interest");
 printf(" rate, and period\n");
 scanf("%f %f %d", &principal, &inrate, &period);
 printline ('*' , 52);
 amount = value (principal, inrate, period);
 printf("\n%f\t%f\t%d\t%f\n\n",principal,
  inrate,period,amount);
 printline('=',52);
 getch();
}
void printline(char ch, int len)
{
 int i;
 for (i=1;i<=len;i++) printf("%c",ch);
 printf("\n");
}
value(float p, float r, int n) /* default return type */
{
 int year;
 float sum;
 sum = p; year = 1;
 while(year <=n)
{
  sum = sum * (1+r);
  year = year +1;
 }
 return(sum);     /* returns int part of sum */
}

No comments:

Post a Comment