search
Log In
19 votes
4k views

Consider the program below in a hypothetical language which allows global variable and a choice of call by reference or call by value methods of parameter passing.         

int i ;
program main ()
{
    int j = 60;
    i = 50;
    call f (i, j);
    print i, j;
}
procedure f (x, y)
{           
    i = 100;
    x = 10;
    y = y + i ;
}

Which one of the following options represents the correct output of the program for the two parameter passing mechanisms?

  1. Call by value : $i = 70, j = 10$; Call by reference :$ i = 60, j = 70$
  2. Call by value : $i = 50, j = 60$; Call by reference :$ i = 50, j = 70$
  3. Call by value : $i = 10, j = 70$; Call by reference :$ i = 100, j = 60$
  4. Call by value : $i = 100, j = 60$; Call by reference :$ i = 10, j = 70$
in Compiler Design
edited by
4k views
4
Why is this marked out of syllabus?
12

2 Answers

44 votes
 
Best answer

Correct answer is (D)

CALL BY VALUE :- $i$ as global variable declared. Then in $main()$ a local variable $j$ as integer declared i.e $j=60$ And global variable $i$ initialized to $50$ by $i=50$. Now procedure $f$ called and values of $i$ and $j$ are passed to it. i.e., in $f(i,j) \rightarrow  f(x, y)$ content of memory location of $i$ (here $50$) is copied to memory location of $x$ (which is different from $i$) and content of memory location of $j$ (here, $60$) is copied to memory location of $y$. Then in $f(x,y)$ $i=100$ changes the global $i$ to $100$,  $X= 10$ changes the local $X$ from $50$ to $10$ and $Y= y+ i$ means $y=60+100=160$. Now when return back to main, $i$ and $j$ will be $100$ and $60$ respectively.

CALL BY REFERENCE:- Now procedure $f$ called and passed reference of $i$ and $j$ to it. i.e., in $f(i,j)  \rightarrow f(x, y)$ $x$ and $y$ are new names (aliases) pointing to the same memory location of $i$ and $j$ respectively. So, $i = 100$ changes the global $i$ to $100$ and $x= 10$ means $x$ as well as global $i =10$ (as the $i$ being passed is the global variable and $x$ and $i$ share the same address).
$y= y+ i$ means $y = 60+10=70$ and this changes the value of $j$ also to $70$ as $j$ and $y$ have the same address. Now when return back to main, $i$ and $j$ will be $10$ and $70$ respectively.


edited by
3
Answer is correct. But

Call by value: "Passed copies" not "passed reference". Let address of i be 1000, and that of j be 2000. Then in f(x,y), address of X will be some 3000 and address of Y will be another. Just that the content of address 1000 is copied to address of X and content of Y is copied to address of Z. And that is why it is called "Call-by-value"
1
Sorry for typo mistake. ..corrected it now
0
@Arjun sir please explain call by referrence too.
0
Beautifully explained.
3 votes

Hope it helps:)

Answer:

Related questions

30 votes
6 answers
1
5.1k views
The following program fragment is written in a programming language that allows global variables and does not allow nested declarations of functions. global int i=100, j=5; void P(x) { int i=10; print(x+10); i=200; j=20; print (x); } main() {P(i+j);} If the ... scoping and call by name parameter passing mechanism, the values printed by the above program are $115, 220$ $25, 220$ $25, 15$ $115, 105$
asked Apr 24, 2016 in Compiler Design jothee 5.1k views
19 votes
3 answers
2
5.5k views
What is printed by the print statements in the program $P1$ assuming call by reference parameter passing? Program P1() { x = 10; y = 3; func1(y,x,x); print x; print y; } func1(x,y,z) { y = y + 4; z = x + y + z } $\text{10, 3}$ $\text{31, 3}$ $\text{27, 7}$ None of the above
asked Sep 15, 2014 in Programming Kathleen 5.5k views
0 votes
1 answer
3
473 views
Show the activation records and the display structure just after the procedures called at lines marked $x$ and $y$ have started their execution. Be sure to indicate which of the two procedures named $A$ you are referring to. Program Test; Procedure A; Procedure B; Procedure A; begin …… end A; begin y: A; end B; begin B; end A; begin x: A; end Test
asked Apr 24, 2016 in Programming jothee 473 views
19 votes
2 answers
4
2.2k views
For the following code, indicate the output if static scope rules dynamic scope rules are used var a,b : integer; procedure P; a := 5; b := 10; end {P}; procedure Q; var a, b : integer; P; end {Q}; begin a := 1; b := 2; Q; Write ('a = ', a, 'b = ', b); end
asked Apr 24, 2016 in Compiler Design jothee 2.2k views
...