{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

CIS 15AG Answers Chapter 4: Functions 1. Why do we need functions in a program? 1. Divide a problem into smaller, more manageable parts. 2. Reuse code. 2. How complex should functions be? In general, functions should be relatively short (not more than one page/screen) and should perform a specific task: one function one task! 3. What is the output displayed by the following program fragment? // Local Declarations int a; int b; double c; double d; // Statements a = 25; c = sqrt( a ); printf("%3d %6.2f\n", a, c ); Answer: sqrt stands for “square root”; do not forget to include math.h whenever you call this function or other math functions. d = -7.4; printf("%5.2f %5.2f", d, fabs(d)); printf("%5.2f %5.2f\n", floor(d), ceil(d)); Answer: fabs – absolute value of a floating-point expression ( math.h ) ceil – the smallest integral value greater than or equal to its argument floor – the largest integral value that is equal to or less than its argument b = rand() % 10; printf("%5d\n", b ); Answer: A random number within the range 0 to 9, printed with a width of 5. 1 2 5 5 . 0 0 - 7 . 4 0 7 . 4 0 - 8 . 0 0 - 7 . 0 0

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

View Full Document
CIS 15AG Answers Chapter 4: Functions 4. Review the following program. a. Draw the structure chart. Answer: main calls fun twice, as it is shown in the structure chart given below. For more information on Structure Chart rules, please review: Program Development, page 13 Designing Structured Programs, page 115 Program 4-8, Page 142 Structure Charts, page 154 Selection in Structure Charts, page 215 Program 5-11, page 207 Loops in Structure Charts, 282 Program 6-15, page 257 #include <stdio.h> int fun ( int ); int main( void ) { int a; int b; b = 4; a = fun(b+1); printf"%5d, %5d\n", a, b); a = 1; a = fun(a); printf"%5d, %5d\n", a, b); return 0; } /* ====================== */ int fun ( int b ) { int a; b = 10 * b; a = b + 1; return a + 2; } b. Circle a function definition. 2 main fun fun
CIS 15AG Answers Chapter 4: Functions Answer: The definition of a function must be given once and only once in a program; it is possible to have more calls, and even more prototype declarations for the same function (in more complex programs), but only one definition is allowed. #include <stdio.h> int fun ( int ); int main( void ) { int a; int b; b = 4; a = fun(b+1); printf"%5d, %5d\n", a, b); a = 1; a = fun(a); printf"%5d, %5d\n", a, b); return 0; } /* ====================== */ int fun ( int b ) { int a; b = 10 * b; a = b + 1; return a + 2; } c. Circle a formal parameter. 3

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

View Full Document
CIS 15AG Answers Chapter 4: Functions Answer: A formal parameter is used in a function definition, as shown below: #include <stdio.h> int fun ( int ); int main( void ) { int a; int b; b = 4; a = fun(b+1); printf"%5d, %5d\n", a, b); a = 1; a = fun(a); printf"%5d, %5d\n", a, b); return 0; } /* ====================== */ int fun ( int b ) { int a; b = 10 * b; a = b + 1; return a + 2; } d. Circle a function call. 4
CIS 15AG Answers Chapter 4: Functions Answer: The same function could be called several times in a program, as needed. #include <stdio.h> int fun ( int ); int main( void ) { int a; int b; b = 4; a = fun(b+1); printf"%5d, %5d\n", a, b); a = 1; a = fun(a); printf"%5d, %5d\n", a, b); return 0; } /* ====================== */ int fun ( int b ) { int a; b = 10 * b; a = b + 1; return a + 2; } e. Circle an actual parameter.

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}