13 votes 13 votes What will be the output of the following program assuming that parameter passing is call by value call by reference call by copy restore procedure P{x, y, z}; begin y:y+1; z: x+x; end; begin a:= b:= 3; P(a+b, a, a); Print(a); end Compiler Design gate1999 parameter-passing normal runtime-environment descriptive + – Kathleen asked Sep 23, 2014 edited Oct 25, 2017 by srestha Kathleen 5.8k views answer comment Share Follow See all 0 reply Please log in or register to add a comment.
Best answer 19 votes 19 votes Call by Value : 3 Call by Reference : 12 Call by Copy-Restore : 12 The $3$ parameter passing mechanisms are simulated in the following 'C' codes. PS: C language only supports Call by Value and even in the case of pointers, the value of the pointer is getting passed explicitly in a pointer variable. This is different from call by reference (say in C++) where this happens implicitly. The following code for Call-by-Reference and Call-by-copy-restore is just a simulation of the parameter passing behaviour and their implementation in any language need not be the same. Call by Value #include <stdio.h> int foo(int x,int y,int z) { y = y+1; z = x+x; } int main(void) { int a = 3; int b = 3; foo(a+b,a,a); printf("%d",a); return 0; } Call by Reference (Call by reference is simulated by passing address in C) : #include <stdio.h> int foo(int *x,int *y,int *z) { *y = *y+1; *z = *x+*x; } int main(void) { int a = 3; int b = 3; int c = a+b; foo(&c,&a,&a); printf("%d",a); return 0; } Call by Copy-Restore: #include <stdio.h> void foo(int *x,int *y,int *z) { *y = *y+1; *z = *x+*x; } int main(void) { int a=3; int b=3; int c=a+b; int d,e; d = c;//copy e = a;//copy foo(&d,&e,&e); a = e;//restore c = d;//restore printf("%d",a); return 0; } Aditya answered Sep 23, 2015 selected May 3, 2021 by gatecse Aditya comment Share Follow See all 9 Comments See all 9 9 Comments reply ravi_ssj4 commented Aug 22, 2016 reply Follow Share Aditya's call by reference program : //Call by Reference(Call by reference is realised by passing address in C) : #include <stdio.h> int foo(int *x,int *y,int *z) { *y = *y+1; *z = *x+*x; } int main(void) { int a = 3; int b = 3; int c = a+b; foo(&c,&a,&a); printf("%d",a); return 0; } Call by copy-restore program : //Call by Copy-restore : #include <stdio.h> void foo(int *x,int *y,int *z) { *y = *y+1; *z = *x+*x; } int main(void) { int a=3; int b=3; int c=a+b; int d,e; d = c;//copy e = a;//copy foo(&d,&e,&e); a = e;//restore c = d;//restore printf("%d",a); return 0; } 3 votes 3 votes Alakhator commented Nov 17, 2019 reply Follow Share I think the answer to call by reference should be 14. Why to create a new variable for a+b. If it call by reference then y=y+1 will make a=4. Then x is calculated as 4+3=7 and then z=7+7 which makes a=14. Is this right @arjun sir can you plz check? 0 votes 0 votes pritishc commented Dec 12, 2019 reply Follow Share The expression $a + b$ is assigned a temporary memory location in the actual program... I think they're known as anonymous expressions, because their locations don't have a name assigned. You can verify call by reference behaviour in the docs of any language that supports it, like C++. 0 votes 0 votes prithatiti commented Jun 17, 2020 reply Follow Share @Aditya @srestha C Programming Language doesn't allow call by reference. 1 votes 1 votes akshay7797 commented Jul 20, 2020 reply Follow Share @prithatiti but it can be implemented by passing address of the variable and then using pointers to modify those variables https://stackoverflow.com/questions/2229498/passing-by-reference-in-c 1 votes 1 votes mohitp commented Jul 24, 2020 reply Follow Share Please correct if iam wrong I think call by copy restore is not implemented as above because there would be no difference between call by reference and call by copy restore. Call by copy restore shows unspecified behaviour when same argument is passed multiple times. http://pages.cs.wisc.edu/~fischer/cs536.s08/course.hold/html/NOTES/9.PARAMETER-PASSING.html 1 votes 1 votes Kiyoshi commented May 7, 2021 reply Follow Share @mohitp Yes you are right. your link is amazing..Understood the whole concept. 1 votes 1 votes MrBananaMan commented Nov 19, 2021 reply Follow Share call by reference function is like macro function in C. answer should be 14. please check and update 0 votes 0 votes John_Smith commented Nov 19, 2023 reply Follow Share @Aditya There seems to be two issues with your approach:- In copy-restore, you seem to be restoring the value of the expression \(a+b\). To the best of my knowledge, copy-restore only restores values for variables/arguments/expressions that have an associated l-value. \(a+b\) doesn’t seem to have an inherent l-value, right? How do you know the order of restoring the values for variables/arguments/expressions is left-to-right? \(12\) seems to be the answer only when the last variable to be restored is \(z\), preceded by \(y\). If we were to restore \(z\), followed by \(y\), then \(a\) seems to possess a value of \(4\) instead. 0 votes 0 votes Please log in or register to add a comment.