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

Consider the following program  is  pseudo-Pascal syntax

program main;
    var x: integer;  
    procedure Q (z: integer);
    begin     
        z := z+x; 
        writeln(z); 
    end; 
    procedure P (y: integer);
    var x: integer;
    begin     
        x := y+2;       
        Q(x);     
        writeln(x); 
    end  
begin  
    x := 5; 
    P(x); 
    Q(x); 
    writeln(x);
end

What is the output of the program when

  1. the parameter passing mechanism is call-by-value and the scope rule is static scoping?  
  2. the parameter passing mechanism is call-by-reference and the scope rule is dynamic scoping?
asked in Programming by Veteran (59.5k points)
edited by | 936 views
0
can someone explain the part b how does it works.

1 Answer

+8 votes
Best answer
  A) Call By Value : 12 7 10 5  B) Call By Reference : 14 14 10 10
answered by Active (3k points)
edited by
+6
Call by reference result should be: 14 14 10 10, because procedure P has its own local x which gets changed on calling it, there is no assignment of y therefore, it will not change the value of global x.

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

39,713 questions
46,750 answers
140,552 comments
58,380 users