I'm confused about problem 2.2 from Quiz 1: "Do compare1 and compare2 print the same thing for all possible inputs? If not, give a pair of inputs for which they print different things." The answer sheet says "No, they print different things for negative inputs. This is because a and b are updated to refer to a different number in compare1, whereas they are not updated in compare2." I'm confused, do they not return the same values for, say, (-2,2)? Is there a pair of inputs for which they print different things? Code: http://codepad.org/IHpscfHL
This one tricked me at first too. The key is to notice that in compare1, a and b are actually assigned to new values, so in your example (-2,2), when a is printed it will print as 2 (NOT as -2). If you look at compare2, a is never actually "assigned" to 2, so when it is printed it will still be -2. They both actually give the same logical answer, it is just that compare2 does it without modifying the original input variables. Hope this helps...
Join our real-time social learning platform and learn together with your friends!