The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+15 votes
653 views

Consider the following pseudo-code (all data items are of type integer): 

procedure P(a, b, c);
    a := 2;
    c := a + b;
end {P}

begin 
    x := 1;
    y := 5;
    z := 100;
    P(x, x*y, z);
    Write ('x = ', x, 'z = ', z);
end

Determine its output, if the parameters are passed to the Procedure P by

  1. value
  2. reference
  3. name
asked in Compiler Design by Veteran (59.5k points)
edited by | 653 views
+1

Note that pass by name is similar to pass by reference in the sense that by substituting the actual parameters into the function body, the function body can both read and write the given parameters. 

Ref: https://stackoverflow.com/questions/838079/what-is-pass-by-name-and-how-does-it-work-exactly

3 Answers

+25 votes
Best answer
  1. Pass by value: Function cannot modify a variable in the calling function. So, 
    $x = 1, z = 100$
     
  2. Pass by reference: An alias of the variable (a different name but having same memory location) is used to pass the variable to a function. So, whatever change occurs for the variable in the called function is reflected in the calling function.
    $x = 2, z = 7 (2 + 5)$
     
  3. Pass by name: The expression used to call a function is copy pasted for each formal parameter. So, the body of P becomes,
    $x := 2;$
    $z := x + x*y;$

    So, printed value will be 
    $x = 2, z = 12$
answered by Veteran (355k points)
edited by
0

@arjun sir,

for call by reference here:

how address of x*y will pass to b in procedure P(a, b, c)??

+8
x*y will be stored in a temporary and reference to that memory will be passed.
0
 Write ('x = ', x, 'z = ', z); ... sir can you plz explain this line as i am not getting it .. what is meant by 'x = ' ?? and why two value not 4 ?
0
nice explanation @Arjun sir
+3 votes

....

answered by Boss (12.7k points)
+2 votes
A) 1) 1,100       2) 2,7    3) 2,12

B)  1) 5,10    2) 1,2
answered by Loyal (6.8k points)

Related questions



Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true

38,115 questions
45,621 answers
132,331 comments
49,306 users