Home | | Schedule | | Assignments | | Lectures | | Resources
Question 1 What are the values of a and b just before call to function Change()? Why do they have these values?
a = 1, b = 2; They are assigned near the top of main.
Question 2 Why does the value of the variable a change after the execution of function Change()?
It is passed as the first parameter to Change(), and that parameter is passed by reference and is modified within the Change() function.
Question 3 Why do the values of variables a and b not change after the execution of the function Change() ?
Removing the ampersand makes it so both parameters to Change() are call-by-value, so Change() only modifies a copy of the variables.
Question 4 If you want the value of the actual parameters to be affected by the changes made in the called function, should the formal parameters be reference or value?
Reference.
Question 5 What warning message do you get and why?
The error message is about invalid initialization of reference, specifically:
args.cc:24: error: invalid initialization of non-const reference of type int& from a temporary of type int
args.cc:15: error: in passing argument 1 of void Change(int&, int)
Only a simple variable can be passed into a reference parameter, but we tried to pass in a literal 1. In other words,
you can't create a reference to a "temporary" value like a literal number or expression, only to a variable.
Home | | Schedule | | Assignments | | Lectures | | Resources
CSCI 131: Techniques of Programming
Last Modified: February 23, 2014
Page Expires: October 12, 2014