CS 1044 Homework 4 Key Spring 1999 Q Ans Reason 1 2 Straight from the notes. 2 1 In this case, the function uses the value of the parameter, but doesn't change the parameter, so you should use pass-by-value. 3 6 The first parameter is passed by reference, so it must be a variable (not a constant or an expression). The second parameter is passed by value, so it can be anything. Answer (1) is syntactically OK even though it does ignore the value returned by the function. 4 3 The parameter alpha should be passed by value (see question 2). The parameter beta is used to get a value out of the function (as well as get a value in, but that doesn't effect the answer here), so beta must be passed by reference. 5 3 The call to Twist() modifies the value of the second actual parameter, s, since it's passed by reference. 6 1 7 5 With pass-by-reference, the ACTUAL parameter and the FORMAL parameter are the same thing. 8 6 9 1 The call to Demo() modifies the second actual parameter (reference pass) but not the first. The rest is just tracing the arithmetic. 10 2 Same as question 9, but now the first actual parameter is passed by reference. 11 5 If no & is used, the parameter is being passed by value or by constant reference. In either case, it can be any of the listed types of things. 12 2 The original value of gamma isn't used by the function, so gamma is not an "into" parameter. gamma is assigned a value (and passed by reference), so it is used for "out of" communication. 13 1 Here gamma is passed by value, which rules out "out of". The original value of gamma (the value of the actual parameter) is used by the function, so gamma is used as an "into" parameter. 14 3 Here gamma is passed by reference, its original value is used by the function, and gamma is assigned a new value by the function. So, gamma is used for "into" and also "out of" communication. 15 2 The value of the parameter beta is assigned to delta (and alpha), so delta must be initialized before the call. The original value of the parameter alpha is not used by the function, so it doesn't matter if alpha has been initialized. The local variable delta cannot be assigned a value before the function is called. 16 2 If alpha were global, then ANY function could access alpha; that rules out answers 1 and 3. If alpha were an actual parameter to F, then alpha would be used in a CALL to F, and so alpha would be declared in the function that calls F and accessible to that function; that rules out answer 4. 17 4 This is a scope question. The assignment statement inside the if statement refers to the variable alpha that is local to the if body (not the one that is declared before the if). So the assignment statement makes beta equal 25 (not 23). The cout statement in the if body will print the alpha that is local to the if body, which is 5 (not 3). The cout statement after the if body will print the alpha that is declared outside the if, which is 3 (not 5). 18 3 The function must return an int value; that eliminates answers 1, 2 and 5. The letter grade must be communicated to the function as a char parameter, and there is no reason to pass it by reference, which eliminates answer 4. But, answer 3 will do. 19 2 The call to Power() modifies the value of the actual parameter pow, since it is being passed by reference. Trace the arithmetic to get the answer. 20 4 This is similar to question 5.