Functions with no argument and no return values

Program 41) Write a program with multiple functions that do not communicate any data between them.


A program with three user-defined function is given below main is the calling functions that calls printline and value functions. Since both the called functions contain no argument, there are no argument declarations. The printline function, when encountered, prints a line with a length of 35 characters as prescribed in the function. The value function calculates the value of principal amount after a certain period of years and prints the results. The following equation is evaluated repeatedly:
                    Value = Principal (1 + interset-rate)


                                 PROGRAM

   /*  Function declaration  */
void printline (void) ;
void value (void) ;
 main ( )
  {
       printline ( ) ;
       value ( ) ;
       printline ( ) ;
 }
/*      Function1 : printline ( )      */

 void printline (void)      /*     contains no argument      */
 {
      int 1 ;
      for (i = 1; i <= 35; i++)
           printf ("%c", '-') ;
      printf ("\n") ;
 }
     /*             Function2 : value ( )         */
 void value (void)           /*    contains no argument      */
  {
          int year, period ;
          float inrate, sum, principal ;

         printf ("Principal amount?") ;
         scanf ( "%f", &principal) ;
         printf ("Interest rate?   ") ;
        scanf ("%f", &inrate) ;
         printf ("Period?    ") ;
         scanf ( "%d", &period) ;

        sum = principal ;
        year = 1 ;
        while (year <= period)
        {
                sum = sum * (1 + inrate) ;
                year = year + 1 ;
       }
       printf ("\n%8.2f %5.2f %5d %12.2f\n", principal,
                               inrate, period, sum) ;
 }


Output:       ---------------------------------------------------------
                     Principal amount?       5000
                     Interest rate?                 0.12
                     Period                            5


                     5000.00         0.12          5          88.71
                    ---------------------------------------------------------


It is important to note that the function value recieves  its data directly from the terminal. The input data include principal amount, interest rate and the period for which the final value is to be calculated. The while loop calculates the final value and  the results are printed by  the library function printf. When the closing brace of value() is reached, the control is transferred back to the calling function main. Since everything is done by the value itself there is in fact nothing left to be sent back to the called function. Return types of both printline and value are declared as void.
Note that no return statement is employed. When there is nothing to be returned, the return statement is optional. The closing brace of the function signals the end execution of the function, thus returning the control, back to the calling function.
 

No comments:

Post a Comment