Chapter 4 Computer Science with C++ Name: Review Worksheet A Mr. Ferwerda Answer the following: 1. The? parameters appear in a function call. 2. The? parameters appear in a function declaration. 3. Formal parameters that are reference parameters must have actual parameters that are?. 4. Formal parameters that are reference parameters must have actual parameters that are of the? data type. 5. Formal parameters that are value parameters must have actual parameters that are?. 6.? parameters make local copies of the actual parameters, and therefore protect the actual parameters from unwanted changes. 7.? parameters make create aliases for the actual parameters, and therefore any changes made to them change the values of the actual parameters. 8.? parameters make create aliases for the actual parameters, but the compiler will not allow any changes to be made to the actual parameters. 9. Which is used to test a function, a driver or a stub? 10. Which type of testing is the process given in the previous problem, top-down testing or bottom-up testing? 11. Which is used to test main program flow, a driver or a stub? 12. Which type of testing is the process given in the previous problem, top-down testing or bottom-up testing? 13. Variables declared within a function are said to be? variables. 14. Variables that can be used anywhere in the main function or in any functions are called? variables. 15. Where are global identifiers declared in a program? 16. A(n)? occurs when a function makes an unwanted change in a global variable. 17. The? of an identifier refers to the largest block in which the identifier is visible (i.e. when it can be referenced). 18. When a problem is broken into smaller subproblems, the process is called?.
Computer Science with C++ - Chapter 4 Review Worksheet A: page 2 19. Local variables cannot be referenced outside of the function in which they are declared. (True / False ) 20. The use of local variables and formal parameters enhances the portability of a function. (True / False ) 21. A function that does not return a value using a return statement is called a? function. 22. A void function never has any parameters. (True / False ) 23. Name the four parts of any function declaration 24. Function declarations should be located? (before / after) the main function in a program. 25. Function implementations should be located? (before / after) the main function in a program. 26. A local variable in a function can have the same name as a program global variable. (True / False ) 27. The facts that a function can assume to be true on entry are called the?. 28. The conditions that must be true after a function's execution are called the?., 29. When doing software engineering with teams, it is more important that the teams know? another team is doing instead of? they are doing it. 30. List all of the data types can be returned as the value of a user-defined function. i. int iii. char v. functions don't return ii. double iv. apstring values of any type 31. A constant declared within a function is global to the entire program. (True /False ) 32. Name the two parts of a programmer-defined library, and give the customary extension given to each. 33. Name the three preprocessor directives that must occur in the library header file.
Computer Science with C++ - Chapter 4 Review Worksheet A: page 3 In 34-41, indicate whether the statement is a VALID or INVALID function declaration; if invalid, explain why: 34. int example ( double x ); 35. int example; 36. int example ( ); 37. void example ( int &a ); 38. double example ( x ); 39. char example ( int a, double &b ); 40. void example ( char a, b ); 41. apstring example ( double &a, double &b, int c ); In problems 42-50, use the following declarations made in the program to indicate whether the function call is a VALID or INVALID if made in the main function; if invalid, explain why: void sample ( int a, double &b, char c ); double r; int j; char c; 42. sample ( j, r, c ); 43. sample; 44. sample ( j, &r, c ); 45. sample ( a, b, c ); 46. sample ( c, j, r ); 47. sample ( 3, 5.0, 'X' ); 48. sample ( 3, r, 'X' ); 49. sample ( j * 3, r - 7, c ); 50. sample ( j, r );
Computer Science with C++ - Chapter 4 Review Worksheet A: page 4 In questions 51-60, use the program skeleton below to answer TRUE or FALSE; if false, explain why: #include <iostream.h> int x, y, z; void one ( int q, int r ); void two ( int t ); void three ( int &r ); void four ( int t ); void one ( int q, int r ) int v; void two ( int t ) void three ( int &r ) int x; void four ( int t ) int z; 51. q is local to one and global to two and three. 52. Global variable y can be accessed by all parts of the program. 53. t can be accessed by one. 54. r can be accessed by two. 55. z is a global variable that is visible to, and can be accessed by, all parts of the program. 56. The statement q = r * x; would be legal if it appeared in the body of three. 57. The statement y = r * x; would be legal if it appeared in the body of three. 58. r is the only reference parameter in the program. 59. three could be called in two. 60. two could be called in four.
Computer Science with C++ - Chapter 4 Review Worksheet A: page 5 In problems 61-65, write function declarations (without the supporting documentation) for the following: 61. Write a function declaration for a function displayinstructions that is passed nothing and returns nothing 62. Write a function declaration for a function getreal that has no parameters and returns a real (double) number. 63. Write a function declaration for a function getstuff that is passed two characters ch1 and ch2, and returns a real number (not through the parameter list). 64. Write a function declaration for a function newvalue that is passed one real number number1 and returns a real number number1 (through the parameter list). 65. Write a function declaration for a function funny that is passed two real numbers v1 and v2, and returns two integer numbers n1 and n2. In 66-68, write the exact output given by the following programs: 66. #include <iostream.h> Output double dostuff ( int p, int q ); int x, y; double a, b; x = 4; y = 7; cout << x = << x << a = dostuff ( x, y ); b = dostuff ( y, x ); cout << a = << a << y = << y << endl; b = << b << endl; double dostuff ( int p, int q ) return ( p 10 ) * q;
Computer Science with C++ - Chapter 4 Review Worksheet A: page 6 67. #include <iostream.h> Output void mixup ( int a, int &b, int &c ); int x, y, z; x = 30; y = 40; z = 50; mixup ( x, y, z ); x = 30; y = 40; z = 50; mixup ( z, y, x ); x = 30; y = 40; z = 50; mixup ( x, x, z ); void mixup ( int a, int &b, int &c ) a = a + 100; b = b + 60; c = c + 70; cout << a << << b << << c << endl; 68. #include <iostream.h> Output void unusual ( int &who, int what ); int who, where; who = 8; where = 2; cout << who << << where << endl; unusual ( who, where ); cout << who << << where << endl; unusual ( where, who ); cout << who << << where << endl; void unusual ( int &who, int what ) who = 5 * what; what = 10 - who; cout << who << << what << endl;
Computer Science with C++ - Chapter 4 Review Worksheet A: page 7 Program Coding: in 69-71, write code for the problem: 69. Write a syntactically correct library header file for a library called geometry.h. Include in the file the declaration for 2 functions called rectarea and rectperimeter. Each function will take two real parameters (length and width) and return, respectively, the area and perimeter of the rectangle with the given length and width. Don t forget to include the necessary preprocessor directives! Be sure to use proper documentation, including all supporting documentation that normally goes with a function declaration. 70. Write the library implementation file that corresponds to the header file described in the previous problem. Only put in the #include statements that are absolutely necessary for the implementation of the functions. 71. On the back of this sheet, write a simple driver program that tests the corresponding library files described in the previous two problems. Only put in the #include statements that are absolutely necessary for the implementation of the functions.