Tuesday, July 28, 2009

C++ x = x + 1?

Suppose that a function uses a reference parameter called x. If the following statement:





Set x = x + 1





is executed inside the function, then the value of the corresponding actual parameter is:











A. increased by 1





B. doubled





C. changed to 1





D. changed to undefined





E. unchanged

C++ x = x + 1?
A. Reference numbers can be altered by functions.





If the parameter were passed by value, then it would be E.
Reply:The answer is A.





What x = x + 1 is saying is:





1) Take whatever x is, add one to it (aka x + 1)


2) Set the new value of x to be the value you got from step one (aka x =)





Example:


If x is 4 and you use x = x + 1 you get:


1) 4 + 1 = 5


2) Set new value of x to 5
Reply:It is A.


As it is using reference the value will be incremented by 1.


No comments:

Post a Comment