Key for Homework 6 on Scope and Parameters Q A Reason ----------------------------------------------------------------------------------- For questions 1 through 5 you need to remember the basic rules for scope: - The scope of an identifier begins where the declaration occurs. - The scope extends to the end of the block, if any, in which the declaration occurs. - The scope of a formal parameter is the body of the function. - "Local" names declared within a block hide declarations of the same name in an enclosing block. 1 5 Straight from the rules. 2 5 Straight from the rules. 3 5 Straight from the rules. 4 1 Straight from the rules. 5 9 6 2 For this to compile, Alpha must be declared somewhere that would put it in scope within the body of the function Calc(). Since there's no local declaration for Alpha, the only alternative is that it must be declared at file (or global) scope. 7 1 The function receives a copy of the actual parameter, placed in the corresponding formal parameter. So, there's no way for the function to modify the actual parameter. Since a copy is passed, the actual parameter could be a variable or a constant or an expression. 8 6 The formal parameter is an alias for the actual parameter, so any change to the formal parameter will also change the actual parameter. When pass-by-reference is used, the actual parameter MUST be a variable. 9 5 Same situation as in question 8, except that the const means that the function is not allowed to modify the formal parameter, so it can't make any changes to the actual parameter. 10 3 Since the data flow for alpha is one-way into the function, it should be passed by value (the function does not need to change the actual parameter). Since the data flow for beta is two-way, the function needs to modify the actual parameter, so beta must be passed by reference. alpha could also be passed by constant reference, but that's not a choice in the given answers.