Chapter 9-p - Example 9.1 Write a program with multiple...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Example 9.1 Write a program with multiple functions that do not communicate any data between them. A program with three user-defined functions is given in Fig.9.4. main is the calling function that calls printline and value functions. Since both the called functions contain no arguments, 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+interest-rate)
Background image of page 1

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

View Full DocumentRight Arrow Icon
FUNCTIONS WITH THE ARGUMENTS, NO RETURN VALUES Program /* Function declaration */ void printline (void); void value (void); main() { printline(); value(); printline(); } /* Function1: printline( ) */ void printline(void) /* contains no arguments */ { int i ; for(i=1; i <= 35; i++) printf("%c",'-'); printf("\n"); } /* Function2: value( ) */ void value(void) /* contains no arguments */ { 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); }
Background image of page 2
Output ----------------------------------- Principal amount? 5000 Interest rate? 0.12 Period? 5 5000.00 0.12 5 8811.71 ------------------------------------ Fig.9.4 Functions with no arguments and no return values Example 9.2 Modify the program of Example 9.1 to include the arguments in the function calls. The modified program with function arguments is presented in Fig.9.7. Most of the program is identical to the program in Fig.9.4. The input prompt and scanf assignment statement have been moved from value function to main. The variables principal, inrate, and period are declared in main because they are used in main to receive data. The function call value(principal, inrate, period); passes information it contains to the function value. The function header of value has three formal arguments p,r, and n which correspond to the actual arguments in the function call, namely, principal, inrate, and period. On execution of the function call, the values of the actual arguments are assigned to the corresponding formal arguments. In fact, the following assignments are accomplished across the function boundaries: p = principal; r = inrate; n = period; FUNCTIONS WITH ARGUMENTS BUT NO RETURN VALUES Program /* prototypes */ void printline (char c);
Background image of page 3

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

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 12

Chapter 9-p - Example 9.1 Write a program with multiple...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online