5 votes 5 votes Consider the following code snippet. void first(int p, int t) { p += t; t += p; } main() { int p = 4; first(p , p); } What is the final value of $p$ in both call by value and call by reference respectively ? $4$ and $12$ $5$ and $12$ $12$ and $16$ $4$ and $16$ Compiler Design tbb-mockgate-2 compiler-design parameter-passing runtime-environment + – Bikram asked Jan 24, 2017 • edited Sep 11, 2020 by ajaysoni1924 Bikram 504 views answer comment Share Follow See all 0 reply Please log in or register to add a comment.
Best answer 7 votes 7 votes In call by value, function first() operate on copy of p, hence value of p remains unchanged. In call by reference, first value of p changes to 4+4, then it changes to 8+8, hence 16. Bikram answered Jan 24, 2017 • selected Feb 3, 2017 by Bikram Bikram comment Share Follow See all 4 Comments See all 4 4 Comments reply Tendua commented Jan 24, 2017 reply Follow Share Can't find x in whole program 1 votes 1 votes Bikram commented Jan 24, 2017 reply Follow Share it is p, corrected , thanks :) 1 votes 1 votes devendra commented Feb 4, 2017 reply Follow Share @Bikram veteran how solve this give picture solution 2 votes 2 votes Bikram commented Feb 4, 2017 reply Follow Share @devendra Here p is Integer, therefore p will store 4. in call by value, p wont change, so p=4. in call by reference, any computation will effect p, here both p and t are referring to same location. p+=t, this will make value of p =8. now of course t is also 8, as both referring to same location. t+=p, this will make final value of t or p as 16. (value of t and p means value refereed by t and p.) Hence it becomes 4 and 16, option D. 2 votes 2 votes Please log in or register to add a comment.