edited by
3,530 views
27 votes
27 votes

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 $\text{P}$ by

  1. value
  2. reference
  3. name
edited by

3 Answers

Best answer
44 votes
44 votes
  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 the 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 $\text{P}$ becomes,
    $x := 2;$
    $z := x + x*y;$

    So, printed value will be  b$x = 2, z = 12$
edited by

Related questions

22 votes
22 votes
3 answers
1
24 votes
24 votes
1 answer
4
Kathleen asked Sep 12, 2014
5,737 views
Match the pairs in the following questions by writing the corresponding letters only.$$\begin{array}{|ll|ll|}\hline \text{(a)} & \text{Buddy system} & \text{(p)} & \tex...