

InterviewSolution
Saved Bookmarks
1. |
Considering the following function definition;The expected, desired output is 5! = 120 What will be the actual output of the program? It is not the same as above, why? What modification are required in the program to get the desired output. |
Answer» The output is 0! = 120 Because the address of variable ‘a’ is given to the variable ‘n’ of the function fact(call by reference method). So the function changes its value (i.e. n- -) to 0. Hence the result. To get the desired result call the function as call by value method in this method the copy of the value of the variable ‘a’ is given to the function. So the actual value of ‘a’ will not changed. So instead of int fact(int &n) just write int fact(int n), i.e., no need of & symbol. |
|